AMQP Workers
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 = ; moduleexports = // 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 = ; // this is the result from amqplib.connectvar connection; AMQPSchema; // Destroy the schema (delete queues and exchanges) AMQPSchema; // Purge the messages but leave the exchanges, queues and bindings alone AMQPSchema;
Consumer
A consumer is an object oriented approach to consuming queues. They can be used directly by instantiating Consumer or via inheritance.
var Consumer = ; // this is the result from amqplib.connectvar connection; var consumer = queue; // Read will be called when an item is being consumed from the queue.consumer { // 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 { };}consumer; // cleanup when your doneconsumer;
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 = ; var myMsg = // will be converted into a buffer woot: true // see amqplib #publish options persistent: true ; // json blobmyMsgbuffer; // application/jsonmyMsgoptionscontentType;
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 = Message = ; // from amqplib #connectvar connection; var tasks = connection; // publish something to the task exchange tasks; // cleanup when your donetasks;
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.