skybet / amqpea

Node.js AMQP client
2 stars 8 forks source link

AMQPea

AMQP made easy

npm version Build Status BSD-3 Licensed

Happy Pea

"Happy Pea" Copyright FancyFerret on DeviantArt

Goals

Quick Start

Install

npm install --save amqpea

All-in-one example:

var amqpea = require('amqpea');

function die(err) {
    throw err;
}

var uri = 'amqp://guest:guest@localhost:5672/%2F';
var amqp = amqpea(uri, { timeout: 2000 });

amqp.on('error', die);
amqp.on('ready', function() {
    amqp.declareExchange({
        name: 'x'
    }, whenExchangeReady);
});
function whenExchangeReady(err) {
    if (err) return die(err);
    amqp.declareQueue({
        name: 'q',
        exclusive: true,
        binding: {
            exchange: 'x',
            keys: ['route']
        }
    }, whenQueueReady);
}
function whenQueueReady(err) {
    if (err) return die(err);
    beginPublishing();
    var consumer = amqp.createQueueConsumerChannel('q', 1);
    consumer.consume('ack', 'exclusive', function(msg) {
        var body = msg.fromJSON();
        console.log("Received: %j", body);
        msg.ack();
    });
}
function beginPublishing() {
    var i = 0;
    var publisher = amqp.createPublishChannel('confirm');
    setInterval(function() {
        publisher.publish('x', 'route', { num: ++i }, function(err) {
            if (err) return die(err);
            console.log("Published message %d", i);
        });
    }, 1000);
}

Examples

More examples can be found in the examples folder.

API Docs

Most of these options correspond directly to an AMQP protocol concept, for more information see the AMQP 0.9.1 reference.

amqpea(urisOrUri, options) => AMQPConnection

Establish a new AMQPConnection instance.

AMQPConnection

Instances represent a connected AMQP client, use the main amqpea export to create an instance.

Event: error(err)

Fired when the server has an error.

The connection object will not be usable after an error has been emitted. By default node.js will exit your program if you don't listen for this event.

Event: close(hadError)

Fired when the server connection has been closed.

Event: ready()

Fired when the server connection is ready to use.

Event: connection-error(uri, err)

Fired for every failed server connection.

When attempting to connect to multiple servers, this is the only way to see why servers are failing. If none of the servers can be connected to, the error event will be fired with the same err as the last connection-error.

amqp.declareExchange(options, callback(err))

Declare an exchange on the server.

To publish to an exchange, use createPublishChannel.

amqp.declareQueue(options, callback(err))

Declare a queue on the server.

declareQueue calls queue.declare followed by queue.bind if the binding attribute is set in options. queue.bind is called once for each key in the binding.keys array with the binding.exchange and binding.arguments as arguments.

If binding is not present in the options or binding.keys is empty then queue.bind will not be called.

amqp.createPublishChannel(confirm) => AMQPPublishChannel

TODO: write this

amqp.createQueueConsumerChannel(name, prefetch) => AMQPQueueConsumerChannel

TODO: write this

amqp.close()

TODO: write this

AMQPPublishChannel

TODO: write this

channel.publish(exchange, key, body, [fields], callback)

TODO: write this

channel.close()

TODO: write this

AMQPQueueConsumerChannel

Represents a channel to be used for consuming messages.

channel.tag

The consumer's tag.

Event: 'error'

Can be thrown if an ack or reject fails. TODO: move these errors into the actions' callbacks.

channel.consume(ack, exclusive, handler(msg))

Begin consuming the queue.

AMQPMessage

msg.delivery

{object} Delivery information, likely to change in future versions.

msg.properties

{object} Message properties, likely to change in future versions.

msg.content

{Buffer} The raw message content.

msg.fromJSON()

Decode a JSON message into an object, may throw an Error.

msg.ack()

Acknowledge the message with the server.

msg.reject()

Reject the message from the server (via basic.reject).

Running the Tests

To run the tests you will need a local AMQP server. The testsuite talks to the broker as well as via the HTTP admin API.

There are environment variables to set which tell the test runner how to connect.

With the appropriate variables set, use npm to run the testsuite.

npm test