AMQP Message Bus
Node.js message bus interface for AMQP servers, such as RabbitMQ.
Features
- Hides the complexity of AMQP client;
- Comes with build-in symmetric message encryption;
- Supports promises + async/await.
Installation
$ npm install amqp-message-bus
Requirements
- Node.js v.7+
Quick start
Create new message bus.
const MessageBus = ; const bus = url: 'amqp://localhost' encryptionKey: 'keep-it-safe';
Connect to AMQP server and subscribe to queue for messages.
await bus; const unsubscribe = await bus; // unsubscribe from queueawait ; // disconnect from busawait bus;
Connect to AMQP server, create queue (if not exists) and send message.
await bus;await bus;await bus;await bus;
Connect to AMQP server, create topic exchange and publish message to route.
await bus;await bus; // note the 2nd argument (i.e. "topic") used to create a topic exchangeawait bus;await bus; // note the 3rd argument (i.e. route.1)await bus; // note the 3rd argument (i.e. route.1)await bus;
API Docs
#constructor(spec) -> MessageBus
Constructs new message bus with the supplied properties.
Arguments
- props (Object) message bus properties (required).
- props.url (string) AMQP server URL (required).
- props.encryptionKey (string) encryption key to use with symmetric encryption (optional).
Example
const bus = url: 'amqp://localhost' encryptionKey: 'keep-it-safe';
#connect() -> Promise
Connects to AMQP server.
Returns
Promise
Example
bus ;
#disconnect() -> Promise
Disconnects from AMQP server.
Returns
Promise
Example
bus ;
#subscribe(queue, listener)
Subscribes to the designated queue for incoming messages.
Arguments
- queue (string) the name of the queue to subscribe to
- listener (Function) listener function, i.e.
function(msg, props, done)
(required).- msg (Object) message body (required).
- props (Object) message meta-data (required).
- done (Function) call done to signal message proccessing is done (required).
Please visit http://www.squaremobius.net/amqp.node/channel_api.html#channel_assertQueue for further info on props
meta-data.
Returns
Promise<Function>
The function returned is the unsubscribe()
method.
Example
const listener = { // do something with msg and props console; // call done when you are done with msg to remove from queue ;}; bus ;
Example using async/await
const unsubscribe = await bus; // unsubscribe when readyawait ;
#sendToQueue(queue, message, props)
Sends the supplied message to the designated queue.
Arguments
- queue (string) the name of the queue to send message to (required)
- message (*) message body; can be any JSON serializable value (required)
- props (Object) message props (optional).
- props.id (string) message ID (optional; defaults to
UUID v4
) - props.priority (integer) message priority, must be between 1 and 10 (optional; defaults to 1)
- props.timestamp (number) message timestamp (optional; defaults to
Date.now()
) - props.type (string) message type (optional)
Returns
Promise
Example
bus ;
Example using async/await
await bus;
#publish(exchange, routingKey, message, props)
Publishes the supplied message to the designated exchange.
Arguments
- exchange (string) the name of the exchange to publish message to (required)
- routingKey (string) the routing key to publish message to (required)
- message (*) message body; can be any JSON serializable value (required)
- props (Object) message props (optional).
- props.id (string) message ID (optional; defaults to
UUID v4
) - props.priority (integer) message priority, must be between 1 and 10 (optional; defaults to 1)
- props.timestamp (number) message timestamp (optional; defaults to
Date.now()
) - props.type (string) message type (optional)
Returns
Promise
Example
bus ;
Example using async/await
await bus;
#unsubscribe(consumerTag)
Unsubscribes the designated consumer.
Arguments
- consumerTag (string) the ID of the consumer to unsubscribe from queue (required)
Returns
Promise
Example
bus ;
#unsubscribeAll()
Unsubscribes all message bus consumers.
Returns
Promise
Example
bus ;
Contribute
Source code contributions are most welcome. The following rules apply:
- Follow the Airbnb Style Guide;
- Make sure not to break the tests.