Kind: Type
Source: packages/microservices/events/rmq.events.ts
Part of: Microservices
RabbitMQ events map for the ampqlip client. Key is the event name and value is the corresponding callback function.
RmqEvents defines the event-to-callback map used by the RabbitMQ (amqplib) client integration. Each key is an event name emitted by the client, and its value is the callback that should run when that event occurs. This type centralizes event handling configuration for RabbitMQ connection and lifecycle events.
Definition
ts{ error: OnErrorCallback; disconnect: VoidCallback; connect: VoidCallback; blocked: OnBlockedCallback; unblocked: VoidCallback; }
Diagram
mermaidgraph LR Client[RabbitMQ / amqplib Client] -->|emits event| EventName[Event name] EventName --> EventsMap[RmqEvents map] EventsMap --> Callback[Registered callback] Callback --> App[Application handling / logging / recovery]
Usage
tsimport type { RmqEvents } from './rmq.events';
const events: RmqEvents = {
connect: () => {
console.log('RabbitMQ connection established');
},
disconnect: () => {
console.warn('RabbitMQ connection closed');
},
error: (error: Error) => {
console.error('RabbitMQ client error:', error);
},
};
// Register the configured handlers with the RabbitMQ client.
for (const [eventName, callback] of Object.entries(events)) {
rmqClient.on(eventName, callback);
}
AI Coding Instructions
- Add event handlers as key-value entries, where the key matches the event name emitted by the RabbitMQ client.
- Keep callbacks focused on event-specific concerns such as logging, reconnecting, cleanup, or notifying application services.
- Ensure error handlers capture and log useful context; unhandled RabbitMQ errors can terminate the process.
- Register the
RmqEventsmap when initializing the RabbitMQ client so handlers are attached before publishing or consuming messages. - Avoid placing long-running business logic directly in event callbacks; delegate substantial work to application services or background workflows.
Was this page helpful?