Skip to content

NatsEvents

reference
1 min readUpdated

Kind: Type

Source: packages/microservices/events/nats.events.ts

Part of: Microservices

Nats events map for the Nats client. Key is the event name and value is the corresponding callback function.

NatsEvents defines the event-to-callback map used by the NATS client. Each key identifies a NATS event, while its value is the handler invoked when that event is emitted, allowing applications to react to connection lifecycle changes or client errors.

Definition

ts
{ disconnect: DefaultCallback; reconnect: DefaultCallback; update: (data?: string | number | ServersChangedEvent) => any; }

Diagram

mermaid
graph LR
  A[NATS Client] -->|emits event| B[NatsEvents map]
  B -->|event name lookup| C[Registered callback]
  C --> D[Application handling]

Usage

ts
import type { NatsEvents } from './nats.events';

const events: NatsEvents = {
  connect: () => {
    console.info('Connected to NATS');
  },

  reconnect: () => {
    console.info('Reconnected to NATS');
  },

  error: (error) => {
    console.error('NATS client error:', error);
  },
};

// Pass the event map to the NATS client configuration or registration layer.
createNatsClient({
  servers: ['nats://localhost:4222'],
  events,
});

AI Coding Instructions

  • Use event names supported by the configured NATS client and provide a callback for each event that requires application-level handling.
  • Keep callbacks lightweight; delegate expensive work to application services, queues, or asynchronous workflows.
  • Always handle connection and error-related events to improve observability and recovery behavior.
  • Preserve the event-name-to-callback map structure when extending this type; do not invoke handlers directly outside the NATS event integration layer.

Was this page helpful?

Download as PDF
NatsEvents — NestJS head-to-head