Table of Contents
- minimal dependencies: only
- promises-based: async/await support makes it easy to use
- small api: 4 methods only, there's not much to learn 😉
- instantiation-able: the library itself is a Class ; get as many independent instance as needed
- events logging: choose your own log transport
- disconnections-proof: auto reconnect/resend when errors happen
Our use case at @RadioFrance
This wrapper was tailored made for our microservices architecture.
We use RabbitMQ as the bus in the publish-subscribe pattern. A message is never sent directly to a queue, it is always sent to a configured exchange.
Every message should be acknowledged by returning a value :
message.REJECT in the message object).
A non-acknowledged message will be redelivered one time to the exchange. A rejected message will be directly deleted or redirected to the dead letter exchange if configured.
Our queues and exchanges are defined by a external engine.
rab-q doesn't provide API to create, assert or delete these.
$ npm install rab-q
const RabQ = ;const rabQ =exchange: 'your_topic_exchange'queues: 'queue_bind_to_exchange';rabQ;rabQ;rabQ;rabQstart;
User to connect on RabbitMQ.
Password associate to username.
Hostname to trying to get connection.
Port of connection.
Selected virtual host.
Exchange to publish messages.
Consumes messages from these queues.
If this option is omitted or has a falsey value, server will create random names.
Defines the number of messages prefetched by channel. Once the max number of messages is reached, RabbitMQ will wait for some messages to be acknowledged before proceeding with new messages.
Defines a delay in milliseconds before a message is rejected with NACK.
Time in milliseconds before trying to reconnect when connection is lost.
Enables auto acknowledgment.
Enables auto reconnection if an error happens while connecting to the server.
Object of function
Function run before each message treatment. If it return a false value, the message is reject.
Starts a connection.
Returns a promise resolved with
true for a successful connection or
false if a connection already exists.
Stops and closes a connection.
rabQ.publish(routingKey, content, headers)
Publishes a message on exchange
A regular expression to match the routing keys of consumed messages.
A message object to send to the exchange.
Adds RabbitMQ headers to the message.
You can provide a property
x-query-token to trace the lifecyle of a request. If not provided a new UUID will be generated.
Adds a Subscriber on consumed messages filtered by the routing key
A regular expression to match on the routing keys of consumed messages.
A function should acknowledge (or not) messages by returning either a value or a resolved promise with value. (
message is a object with following properties:
string: the constant returned for a positive acknowledgment
string: the constant returned for a negative acknowledgment. NACK will re-queuing message one time.
string: the constant returned for a rejection without redelivery.
Object: content of the received message
string: routing key of the message
string: queue name where the message is consumed
string: a UUID to identify the message
number: timestamp when the message is consumed
Emits a log object with the following properties:
string: can be
string: the UUID to identify the message
string: the message,
Object: the original error if log level is error
Emits an Error if something goes wrong with the connection. If you don't catch this event the process will sto