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).
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.jsvar Schema = require'amqpworkers/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 schemavar AMQPSchema = require'./my_schema';// this is the result from amqplib.connectvar connection;AMQPSchemadefineconnectionthen//;// Destroy the schema (delete queues and exchanges)AMQPSchemadestroyconnectionthen// messages and queues are gone! Good for testing;// Purge the messages but leave the exchanges, queues and bindings aloneAMQPSchemapurgeconnectionthen// messages and queues are gone! Good for testing;
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.connectvar connection;var consumer = queue;// Read will be called when an item is being consumed from the queue.// 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;consumerconsume'the queue name'// optional prefetch optionprefetch: 1;// cleanup when your doneconsumerclose;
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
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
var Message = require'amqp/message';var myMsg =// will be converted into a bufferwoot: true// see amqplib #publish optionspersistent: true;// json blobmyMsgbuffer;// application/jsonmyMsgoptionscontentType;
Messages are only useful in conjunction with Publisher's
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 #connectvar connection;var tasks = connection;// publish something to the task exchangetaskspublish'tasks' // exchange name'request' // routing keywoot: true persistent: truethen// confirmed// rejected;// cleanup when your donetasksclose;
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.