5.3.6 • Published 7 months ago

@klicksite/roger-rabbit v5.3.6

Weekly downloads
46
License
MIT
Repository
github
Last release
7 months ago

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, /* fields */) => {
    // 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

OptionDescriptionRequiredDefault
channelMaxnumber max of channels (max 3)no3
publisherpublisher objectno_
publisher.defaultcreate publish channel without confirmationnotrue
publisher.confirmationcreate publish channel with confirmationnofalse
consumerconsumer objectno_
consumer.defaultcreate consume channelnotrue
prefetchchannel prefetch countno1

Exchange options

OptionDescriptionDefault
typedirect, topic, fanoutempty string (deafault)
nameexchange namenull
optionsoptions used in assertExchangenull

Queue options

OptionDescriptionDefault
namequeue namenull
optionsoptions used in assertQueuenull

Binding options

OptionDescriptionDefault
exchangeexchange namenull
routingKeyrouting key namenull
5.3.6

7 months ago

5.3.5

2 years ago

5.3.4

3 years ago

5.3.3

3 years ago

5.3.2

3 years ago

5.3.3-alfa

4 years ago

5.3.2-alfa

4 years ago

5.2.5-alfa

4 years ago

5.3.1

4 years ago

5.3.0

4 years ago

5.2.6

4 years ago

5.2.5

4 years ago

5.2.4

4 years ago

5.2.3

4 years ago

5.2.2

4 years ago

5.2.1

4 years ago

5.2.0

4 years ago

5.0.11

4 years ago

5.0.10

4 years ago

5.0.9

4 years ago

5.0.8

4 years ago

5.0.7

4 years ago

5.0.6

4 years ago

5.0.4

4 years ago

5.0.3

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.1

4 years ago