amqpworkers

0.0.3 • Public • Published

AMQP Workers

Build Status

AMQP Workers is an opinioned library which codifies a lot of my personal tastes while working with AMQP in JavaScript (node). It also embraces a Promise only api (everything should return a promise) and requires you to setup / manage your own amqp connection (through amqplib).

The primary export is build out of four smaller modules (and while you can use the top level export using the longer form is probably what you want).

Schema

A "Schema" is a blueprint to build all queues, exchanges and bindings between them. Generally you always need to define a schema and its a good idea to try to build it before publishing new messages or consuming a queue.

 
// your_schema.js
 
var Schema = require('amqpworkers/schema');
 
module.exports = new Schema({
  // see examples/schema_config.json
});
 

Now that the schema is defined we can use it to define, purge and destroy it. In RabbitMQ 3.2 and greater all are idempotent but deletes will fail if the queue/exchange does not exist in earlier versions.

// Define the schema
 
var AMQPSchema = require('./my_schema');
 
// this is the result from amqplib.connect
var connection;
 
AMQPSchema.define(connection).then(
 //
);
 
// Destroy the schema (delete queues and exchanges)
 
AMQPSchema.destroy(connection).then(
 // messages and queues are gone! Good for testing
);
 
// Purge the messages but leave the exchanges, queues and bindings alone
 
AMQPSchema.purge(connection).then(
 // messages and queues are gone! Good for testing
);

Consumer

A consumer is an object oriented approach to consuming queues. They can be used directly by instantiating Consumer or via inheritance.

var Consumer = require('amqpworkers/consumer');
 
// this is the result from amqplib.connect
var connection;
 
var consumer = new Consumer(queue);
 
// Read will be called when an item is being consumed from the queue.
consumer.read = function(content, message) {
  // content is the parsed content of the message
  // and message is un mutated value from amqplib
 
 
  // the promise is optional but highly recommended if you care about
  // ack / nack. When this promise is accepted an ack will be sent
  // (and you guessed! nack when rejected).
  return new Promise(function(accept, reject) {
 
  });
}
consumer.consume('the queue name', {
  // optional prefetch option
  prefetch: 1
});
 
// cleanup when your done
consumer.close();

The consumer has the parseMessage method which will be called prior to passing the result of that function and the message along to the .read method. This can be used as a hook for implementing other de-serializing protocols.

Message

A message is a simple representation of an outbound (to be published) message.

Messages are simply any object with a .buffer [Buffer] property and an .options [Object] property. The provided object will parse objects into json blobs (new Buffer(JSON.stringify(obj)) and stamp them with contentType application/json

var Message = require('amqp/message');
 
var myMsg = new Message(
  // will be converted into a buffer
  { woot: true },
 
  // see amqplib #publish options
  { persistent: true }
);
 
// json blob
myMsg.buffer;
 
// application/json
myMsg.options.contentType;

Messages are only useful in conjunction with Publisher's #publish method.

Publisher

Publishers are fairly simple wrappers around #publish and confirm channels. The assumption is that every message is critical and slowing down the publishing process to confirm writes is more important then raw speed.

var Publisher = require('amqpworker/publisher'),
    Message = require('amqpworker/message');
 
// from amqplib #connect
var connection;
 
var tasks = new Publisher(connection);
 
// publish something to the task exchange
 
tasks.publish(
  'tasks', // exchange name
  'request', // routing key
  new Message({ woot: true }, { persistent: true })
).then(
  function() {
    // confirmed  
  },
  function() {
    // rejected
  }
);
 
// cleanup when your done
tasks.close();

License

The MIT License (MIT)

Copyright (c) 2013 Sahaja James Lal

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i amqpworkers

Weekly Downloads

1

Version

0.0.3

License

MIT

Last publish

Collaborators

  • lights-of-apollo