Giter Club home page Giter Club logo

roger-rabbit's Introduction

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
prefetch channel prefetch count no 1

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

roger-rabbit's People

Contributors

vitornogueira avatar victorhmf avatar dependabot[bot] avatar

Stargazers

Roman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.