Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

roger-rabbit

2.1.1 • Public • Published

Travis Codecov npm npm

Roger Rabbit

Roger Rabbit is a module that makes the process of consuming and publishing messages in message brokers easier. It is a wrapper for amqplib.

Install

npm install roger-rabbit --save

Example

// 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);

Documentation

Broker

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

Exchange options

Option Description Default
type direct, topic, fanout empty string (deafault)
name exchange name null
options options used in assertExchange null

Queue options

Option Description Default
name queue name null
options options used in assertQueue null

broker.consume

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

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

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 */);

Install

npm i roger-rabbit

DownloadsWeekly Downloads

5

Version

2.1.1

License

ISC

Unpacked Size

24.1 kB

Total Files

20

Last publish

Collaborators

  • avatar