RabbitMQ
RabbitMQ Channel Integration
The @nestjstools/messaging-rabbitmq-extension
provides seamless integration with RabbitMQ for asynchronous and synchronous message processing in NestJS applications.
π¦ Installation
Install both core messaging and the RabbitMQ extension:
π§© Basic Configuration Example
π Exchange Types
TOPIC
Route messages using wildcard-based routing keys (my_app.command.#
).
DIRECT
Exact routing match. You must define matching bindingKeys
.
FANOUT
Broadcasts messages to all queues bound to the exchange, regardless of routing key.
π Cross-Language Messaging
You can publish messages from other services (non-NestJS apps) by following these rules:
Send a Message to the appropriate queue.
Set Header:
messaging-routing-key
should match the handler:
πͺ¦ Dead Letter Queue (DLQ) β How It Works
When deadLetterQueueFeature: true
is enabled on an AmqpChannelConfig
, the system automatically handles failed messages by routing them to a dedicated "dead letter" queue instead of discarding them or causing application crashes.
Behavior:
Message Handling Fails If a message handler throws an unhandled exception, the message is not acknowledged (
nack
) and is redirected to the DLQ.DLQ Naming Convention The DLQ is created automatically and typically named by appending
dead_letter_queue
to the original queue name. Example: If your queue ismy_app.command
, the dead letter queue will bemy_app.command.dead_letter_queue
.Message Retention Failed messages remain in the DLQ until manually processed, examined, or retried.
Retry Strategy You can manually re-publish messages from the DLQ back to the original exchange with the same routing key (or via tooling or scripts) when you're ready to retry.
π Example Use Case:
π§ Configuration Table: AmqpChannelConfig
AmqpChannelConfig
name
Name of the channel (e.g., 'amqp-command'
).
(required)
connectionUri
RabbitMQ connection URI (e.g., 'amqp://guest:guest@localhost:5672/'
).
(required)
exchangeName
Exchange name in RabbitMQ.
(required)
bindingKeys
Routing keys for queue bindings (e.g., ['my_app.command.#']
).
[]
exchangeType
Type of RabbitMQ exchange (TOPIC
, FANOUT
, DIRECT
).
(required)
queue
Name of the queue to consume from.
(required)
autoCreate
Automatically create exchanges, queues, and bindings if missing.
true
enableConsumer
Enable message consumption from this channel.
true
enableWorker
Enables the internal worker that processes messages. If false
, messages are ignored.
true
avoidErrorsForNotExistedHandlers
Skip errors when no handler exists for a routed message. Useful for optional event handlers.
false
middlewares
Middleware pipeline for pre-processing messages.
[]
normalizer
Attach a normalizer for custom serialization (e.g., Protobuf, Base64).
undefined
deadLetterQueueFeature
Enables capturing failed messages into a DLQ.
false
βοΈ Custom Routing with AmqpMessageOptions
You can customize routing at dispatch time:
Mapping Messages in RabbitMQ Channels
RabbitMQ uses different exchange types to route messages based on routing keys and bindings. Hereβs how message routing works for each exchange type in the context of messaging channels:
Topic Exchange
Topic exchanges route messages based on pattern-matching in the routing key.
Use wildcards like
#
and*
in your binding keys for flexible routing.Example: If you bind your queue with
my_app.command.#
, messages with routing keys such asmy_app.command.user.create
ormy_app.command.system.shutdown
will be routed to that queue.β This is ideal for structured, hierarchical routing across many message types.
Direct Exchange
Direct exchanges use exact matching between the routing key and the binding key.
Ensure that your queue has binding keys explicitly defined.
If no binding key is provided, RabbitMQ defaults to the routing key specified in the message handler.
Use this when you need precise, one-to-one message routing.
Fanout Exchange
Fanout exchanges broadcast messages to all queues bound to the exchange, ignoring routing keys entirely.
Every bound queue receives the message.
Best used for scenarios like logging, notifications, or pub-sub events where all consumers should receive the message.
Last updated