waterline-rabbitmq

0.51.0 • Public • Published

Waterline RabbitMQ Adapter

NPM version Build status Dependency Status Code Climate

RabbitMQ Adapter for Sails and Waterline (AMQP 0.9). Implements the Waterline pubsub interface. The RabbitMQ Adapter does not support persistence on its own, and should always be used with another adapter, such as sails-mongo or sails-postgresql. This module is maintained in collaboration with Michigan Community College Association.

Install

$ npm install waterline-rabbitmq --save

Configure

1. Setup Connection

// config/connections.js
module.exports.connections = {
  regularPostgres: {
    // ...
  },
  rabbitCluster: {
    adapter: 'waterline-rabbitmq',
 
    /**
     * The url of your rabbitmq installation
     */
    url: 'amqp://localhost:5672',
 
    /**
     * Define how persistence is managed. 'true' will subscribe to all queues
     * and persist models that are published as messages. 'false' will do
     * nothing. This lets you turn off the persistence worker feature on the
     * Sails.js web server, and enable it in separate worker processes.
     */
    persistence: true
  }
};

2. Setup Models

For Models that you'd like to be able to publish and subscribe to, add the waterline-rabbitmq connection to the relevant Models, and define a routingKey.

// api/models/Message
module.exports = {
  connection: [ 'rabbitCluster', 'regularPostgres' ],
  routingKey: [ 'stream', 'parentMessage' ],
  attributes: {
    title: 'string',
    body: 'string',
    stream: {
      model: 'stream'
    },
    parentMessage: {
      model: 'message'
    }
    // ...
  }
};

routingKey

The routingKey determines how messages are routed to RabbitMQ queues. Consider an example Message object from above:

{
  title: 'yo dawg',
  body: 'I heard you like messages',
  stream: 'random',
  parentMessage: 1234
}

The [ 'stream', 'parentMessage' ] routingKey would generate a RabbitMQ Routing Key with the value random.1234.

3. Set Primary Key Format

The primary key datatype for the persistence store defaults to 'integer'. You may need to change this, for example mongodb uses strings for their primary key. This is optional depending upon your persistence store.

// config/rabbitmq.js
module.exports.rabbitmq = {
    pkFormat: 'string'
};

Usage

.create(values, callback)

.update(criteria, values, callback)

The .create() and .update() methods can be called per usual on RabbitMQ-enabled models. RabbitMQ dispatches a message to an available Persistence Worker, wherein the object is created or updated by the persistence connection (e.g. regularPostgres above), and returned to the provided callback (or Promise).

Low-level API

"Low-level" is a nice way of saying "only use these methods if you know what you're doing".

Model.getSubscribeSocket(options)

Open a rabbit.js SUBSCRIBE socket to your favorite model.

@param @description required
options.where search criteria no

Example

Message.getSubscribeSocket({ where: { stream: 'myStream' } })
  .then(function (socket) {
    socket.on('data', function (data) {
      var message = JSON.parse(data);
      // see, I told you it was low-level
      
      // ...
    });
  });

Model.getWorkerSocket(options)

@param @description required
options.name worker name (must match that of some 'PUSH' socket) yes
Message.getWorkerSocket({ name: 'encryptionWorker' })
  .then(function (socket) {
    socket.on('data', function (data) {
      var message = JSON.parse(data);
      // ...
 
      socket.ack()
    });
  });

License

MIT

Maintained By

Package Sidebar

Install

npm i waterline-rabbitmq

Weekly Downloads

6

Version

0.51.0

License

MIT

Last publish

Collaborators

  • ryanwilliamquinn
  • tjwebb