Roger Rabbit is a module that makes the process of consuming and publishing messages in message brokers easier. It is a wrapper for amqplib.
npm install roger-rabbit --save
// broker.js
const Broker = require('roger-rabbit');
module.exports = Broker({
host: 'amqp://guest:guest@localhost:5672',
exchange: {
type: 'direct',
name: 'exchange.name',
},
});
// consumer.js
const broker = require('./broker');
const queue = {
name: 'queue.name',
options: {
durable: true,
},
};
const routingKey = 'routing.key.name';
broker.consume({ queue, routingKey }, (message) => {
// do something
// throw an error to reject message
});
// publisher.js
const broker = require('./broker');
broker
.publish('routing.key.name', { message: 'hello world' })
.then(console.log)
.catch(console.error);
Option | Description | Required | Default |
---|---|---|---|
host | message broker connection url | yes | null |
logger | logger object | no | console |
disableLog | disable log (all levels) | no | false |
exchange | exchange options | no | null |
queue | queue options | no | null |
Option | Description | Default |
---|---|---|
type | direct, topic, fanout | empty string (deafault) |
name | exchange name | null |
options | options used in assertExchange | null |
Option | Description | Default |
---|---|---|
name | queue name | null |
options | options used in assertQueue | null |
broker.consume
expects to receive an object with consumers options and routing key name and callback. Example:
const broker = require('./broker');
const queue = {
name: 'queue.name',
options: {
durable: true,
},
};
const routingKey = 'routing.key.name';
broker.consume({ queue, routingKey }, (message) => {
// do something
// throw an error to reject message
});
broker.publish
expects to receive routing key, message and publish options. Example:
const options = {
persistent: true,
exchange: {
name: 'exchange.name',
},
};
broker.publish('routing.key', { message: 'message' }, options)
.then(message => /* handle success */)
.catch(error => /* handle error */);
broker.sendToQueue
expects to receive queue name, message and publish options. Example:
const queue = {
options: {},
};
broker.sendToQueue('queue.name', { message: 'message' }, { queue })
.then(message => /* handle success */)
.catch(error => /* handle error */);