Node.js AMQP for version 0.9.1

Ryan Dahl's node-amqp is great but I needed support for the new headers type of exchange, so I updated it to support AMQP 0.9.1.

Update: node-amqp trunk (https://github.com/postwait/node-amqp) merged my changes, no need to use node-amqp-091 anymore. I assume the examples below still work though I haven't tested them.

Here are a couple examples showing how to use the new bind_headers method I introduced. First a client that subscribes to a queue which uses the headers:

#!/usr/bin/env node
# Use node-amqp for AMQP 0.9.1.
var amqp = require('node-amqp-091');
var util = require('util');

var connection = amqp.createConnection({ host: 'localhost' });

# Wait for connection to become established.
connection.addListener('ready', function () {
  util.debug('Connected');
  # Create a queue and bind to all messages.
  var q = connection.queue('my-queue');
  # Catch all messages
  # Use the default 'amq.headers' exchange
  # Note: this example only filters on one value, add yours.
  # x-match can be either 'any' or 'all'.
  q.bind_headers({"x-match":'any',where:'here'});

  # Receive messages
  q.subscribe(function (message) {
    # Print messages to stdout
    util.debug(util.inspect(message));
  });
});

Then here is an example for a provider that sends messages to that queue:

#!/usr/bin/env node

var amqp = require('node-amqp-091');
var util = require('util');

var connection = amqp.createConnection({ host: 'localhost' });

# Wait for connection to become established.
connection.addListener('ready', function () {
  util.debug('Connected');
  # Connect to the default 'headers' exchange
  var exch = connection.exchange('amq.headers');
  exch.addListener('open', function () {
    util.debug('Open');
    while(true) {
      body = {name:'bob',difficulty:'alive'};
      options = {headers:{name:'bob',where:'here',when:'now'}};
      # The key will not be used in a headers exchange.
      exch.publish('test.bob.is.alive',body,options);
    }
  });
});

posted: 2010-12-23 16:35

Written on December 23, 2010