0.9.0 • Published 6 years ago

hapi-rabbitmq v0.9.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

hapi-rabbitmq Build Status npm version

A HAPI server plugin exposing RabbitMQ-backed PubSub and task queue pattern methods from librabbitmq.

Configuration

The only required config is the url for your RabbitMQ server:

server.register({
  register: require('hapi-rabbitmq'),
  options: {
    url: 'amqp://localhost'
  }
});

This plugin sets two notable defaults, intended to give good results on a HAPI server, where you are probably registering many decoupled plugins, and probably might not know which first set up your RabbitMQ connection.

{
  preserveChannels: true,
  connection: {
    useExisting: true
  }
}

There are many more configuration options which are passed through to librabbitmq. Read more about them.

Usage

Generally speaking, you only need to create a connection once, and that will be reused for all of your channel creation. You do have the option of creating multiple connections and passing those to the methods that create channels, if you need greater control.

PubSub

// Register plugin... start server

const {rabbitmq} = server.methods;

const subscriber = function (message) {
  return new Promise(() => {
    console.log('Message: ', message.payload);
  });
};

rabbitmq.createConnection()
  .then(() => {
    return rabbitmq.addSubscriber({
      exchange: 'pubsub',
      subscriber
    });
  })
  .then(() => next())
  .catch(next);


server.route({
  method: 'POST',
  path: '/publish',
  handler(request, reply) {
    rabbitmq.publishMessage({
      exchange: 'pubsub',
      topic: 'request',
      payload: request.payload
    })
    .then(() => reply('ok'))
    .catch(reply);
  }
});

Task queue

// Register plugin... start server

const {rabbitmq} = server.plugins;
const {ACK} = server.plugins['hapi-rabbitmq'].constants;

const worker = function (task) {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log(task.payload);
      resolve(ACK);
    }, 1000);
  });
};

rabbitmq.createConnection()
  .then(() => {
    return rabbitmq.addWorker({
      queue: 'work',
      worker
    });
  })
  .then(() => next())
  .catch(next);

server.route({
  method: 'POST',
  path: '/work',
  handler(request, reply) {
    rabbitmq
      .pushTask({
        queue: 'work',
        type: 'foo',
        payload: request.payload
      })
      .then(() => reply('ok'))
      .catch(reply);
  }
});

Requirements

  • node.js >= 6.0
  • RabbitMQ 3.6.11 (only version tested)
0.9.0

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.3

6 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.9

7 years ago

0.5.8

7 years ago

0.5.7

7 years ago

0.5.6

7 years ago

0.5.5

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago