hapi-rabbit

0.0.4 • Public • Published

hapi-rabbit

A simple hapijs plugin to connect to rabbitMQ

Build Status Coverage Status npm version Built with Grunt

NPM

Introduction

hapi-rabbit basically wraps rabbit.js and gives the user a simple api to publish and subscribe to rabbitMQ

Prerequisites

  • RabbitMQ
  • Node.js
  • Hapi.js

Installation

npm install hapi-rabbit --save
  • add plugin to hapi server
  • include in your code

Examples

load plugin

plugin.register([
    {
        plugin: require('hapi-rabbit'),
        options: { 
            url: 'amqp://localhost'
        } 
    }
], function (err) {
    if (err) {
        throw err;
    }
});

publish a message

function (request, reply) {
 
    var rabbit = request.server.plugins['hapi-rabbit'];
    rabbit.createContext(function(err, context){
        if(err){
            console.log('err', err);
        }
 
        rabbit.publish(context, 'exchange', 'messageType', 'message', function(err, data){
            console.log('messageObject', data);
        });
    });
    
    reply('Hello!');
}

subscribe

exports.register = function (plugin, options, next) {
 
    plugin.events.once('start', function () {
 
        var rabbit = plugin.plugins['hapi-rabbit'];
        rabbit.createContext(function(err, context){
            if(err){
                console.log('err', err);
            }
 
            rabbit.subscribe(context, 'exchange', function(err, message){
                console.log('message', message);
            });
        });
 
    });
 
    next();
};

Contribute

If you want to contribute to hapi-rabbit, please send me a pull request.

Package Sidebar

Install

npm i hapi-rabbit

Weekly Downloads

1

Version

0.0.4

License

ISC

Last publish

Collaborators

  • aduis