AMQP Workers
![Build Status](https://travis-ci.org/lightsofapollo/amqpworkers.png)
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.
var Schema = require('amqpworkers/schema');
module.exports = new Schema({
});
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.
var AMQPSchema = require('./my_schema');
var connection;
AMQPSchema.define(connection).then(
);
AMQPSchema.destroy(connection).then(
);
AMQPSchema.purge(connection).then(
);
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');
var connection;
var consumer = new Consumer(queue);
consumer.read = function(content, message) {
return new Promise(function(accept, reject) {
});
}
consumer.consume('the queue name', {
prefetch: 1
});
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(
{ woot: true },
{ persistent: true }
);
myMsg.buffer;
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');
var connection;
var tasks = new Publisher(connection);
tasks.publish(
'tasks',
'request',
new Message({ woot: true }, { persistent: true })
).then(
function() {
},
function() {
}
);
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.