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

@klicksite/roger-rabbit

5.2.1 • Public • Published

Roger Rabbit

Travis Codecov npm npm

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 @klicksite/roger-rabbit --save

Documentation

Broker

Broker expect to receives a rabbitMQ connection string and broker options. Example:

// broker.js
const Broker = require('roger-rabbit');
 
const broker = new Broker('amqp://guest:guest@localhost:5672');
 
module.exports = broker;

broker.init

Use broker.init to initialize broker, creating connections and channels based on broker options. Example:

// main.js
const broker = require('./broker');
 
(async() => {
  await broker.init();
})();

broker.assertExchanges

Use broker.assertExchanges to create or check exchanges. It expects to receive an array of exchanges (exchange options). Example:

const broker = require('./broker');
 
const exchanges = [
  { name: 'user_events', type: 'direct' },
  { name: 'repo_events', type: 'topic', options: { durable: true } },
  { name: 'commit_events', type: 'fanout' },
];
 
(async() => {
  await broker.assertExchanges(exchanges);
})();

broker.consume

broker.consume expects to receive an object with queue and array of bindings and callback. Example:

const broker = require('./broker');
 
const queue = {
  name: 'queue.name',
  options: {
    durable: true,
  },
};
 
const bindings = [
  { exchange: 'user_events', routingKey: 'user_events.routingKey' },
  { exchange: 'repo_events', routingKey: 'repo_events.routingKey' },
];
 
(async() => {
  await broker.consume({ queue, bindings }, (message) => {
    // do something
    // throw an error to reject message
  });
})();

broker.publish

broker.publish expects to receive exchange, routing key, message and publish options. Example:

const options = { persistent: true };
 
broker.publish({
    exchange: 'user_events',
    routingKey: 'user_events.routingKey',
    message: { message: 'message' },
    options,
  })

broker.channels

Use broker.channels to get channels. Examples:

const broker = require('./broker');
let context;
let channel;
 
//consumer
context = 'consumer';
channel = broker.channels[context].default;
 
//publisher
context = 'publisher';
channel = broker.channels[context].default; // or broker.channels[context].confirmation;

Broker options

Option Description Required Default
channelMax number max of channels (max 3) no 3
publisher publisher object no _
publisher.default create publish channel without confirmation no true
publisher.confirmation create publish channel with confirmation no false
consumer consumer object no _
consumer.default create consume channel no true

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

Binding options

Option Description Default
exchange exchange name null
routingKey routing key name null

Install

npm i @klicksite/roger-rabbit

DownloadsWeekly Downloads

61

Version

5.2.1

License

MIT

Unpacked Size

71.2 kB

Total Files

113

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar