/busterbunny.js

Opinionated EventBus Library for amqplib

Primary LanguageJavaScriptMIT LicenseMIT

busterbunny.js

Opinionated EventBus Library for amqplib

npm Coverage Status Build Status

Installation

npm install busterbunny

Dev Setup

npm install -g mocha
npm install -g istanbul
npm install coveralls
npm install mockery

Unit Testing

Testing can be run using the following command

npm run test

Code Coverage

Code Coverage provided by Instanbul with hooks for coveralls. To see coverage report run

npm run cover

Usage

var BusterBunny = require('busterbunny');

//example config
var config = {
    amqp: {
        cluster: {
            host: 'host.host.it',
            port: 5672,
            vhost: '/',
            login: 'someguy',
            password: '2insecure',
            heartbeat: 10
        },
        queues: [
            {
                name: 'i.read.from.this1'
            },
            {
                name: 'i.read.from.this2'
            }
        ],
        exchange: 'i.write.2.this1'
    }
};

//init buster bunny
var busterBunny = new BusterBunny(config.amqp);

//raise event against bus
//this will be done when connection and channel is available
busterBunny.raiseEvents('kicked.bucket.1001', { data: { count : 9001 } });

//subscribe to events from bus
//this will be done when connection and channel is available
busterBunny.onNextEvent(function(event) {
    console.log("I found a " +  event.type + " event!");
});

Events

Buster Bunny is an event emitter so it allows you to hook into the object to do things such as logging.
Buster Bunny provides events as a frozen object within buster bunny.

For example if you wanted to log warnings coming out of busterbunny

//This assumes you have required everything and have a logger
var busterBunny = new BusterBunny(config);

busterBunny.on(busterBunny.EVENTS.WARNING_RAISED, function(msg) {
    logger.warn(msg);
});

The current list of events (the property names) are ...

  1. WARNING_RAISED when a warning (like when a threshold is reached) has been reached
  2. READY when buster bunny has successfully established or re-established a connection and channels are available
  3. CONNECTING when buster bunny is establishing connections
  4. RECONNECTING after a connection has been lost but before it has been reconnected
  5. CONNECTED after a connection has been established
  6. AMQP_ERROR when the amqplib throws an error
  7. PUBLISH_CHANNEL_ESTABLISHED when buster bunny is ready to publish events to an exchange
  8. PUBLISH_REQUESTED when an event has been raised with buster bunny
  9. EVENT_RECEIVED when buster bunny has received an event from amqp
  10. EVENT_ACKED when buster bunny has been asked to acknowledge an event
  11. EVENT_NACKED when buster bunny has been asked to reject or requeue an event

Some Opinions To Be Aware Of

  • The library tries to ALWAYS be connected to amqp over one connection with 1 channel for publishing and 1 for consuming.
  • The library also WARNS BUT DOENS'T REJECT when thresholds are hit allowing applications to handle the warning gracefully.
  • The library doesn't enforce the format of event messages.
  • The library does want all events to at least have an identifier and data.