Skip to content

Instantly share code, notes, and snippets.

@mvolkmann
Created May 17, 2012
Embed
What would you like to do?
RabbitMQ client code
var amqp = require('amqp');
var count = 3;
var exchangeName = 'apto';
var pubQueue = 'pong-queue';
var subQueue = 'ping-queue';
var connOptions = {
host: 'localhost',
port: 5672,
login: 'guest',
password: 'guest',
vhost: '/'
};
var exchangeOptions = {
type: 'direct', // options are 'fanout', 'direct' and 'topic'
passive: false, // can create exchange
durable: false, // purged if server restarts
autoDelete: false // don't delete exchange when no queues are bound to it
};
var queueOptions = {
passive: false,
durable: false, //true,
exclusive: false,
autoDelete: true //false
};
function publish(exchange, msg) {
var routingKey = pubQueue;
exchange.publish(routingKey, msg);
count--;
}
var conn = amqp.createConnection(connOptions);
conn.on('ready', function () {
console.log('connection is ready');
conn.exchange(exchangeName, exchangeOptions, function (exchange) {
console.log('exchange is ready');
//exchange.destroy(); // must destroy and recreate to change type
//conn.queue(subQueue, queueOptions, function (q) {
conn.queue(subQueue, function (q) {
console.log('got', q.name);
q.bind(exchange, subQueue, subQueue);
console.log('bound queue to exchange');
q.subscribe(function (msg) {
console.log('got', msg.data.toString());
if (count === 0) {
conn.end();
} else {
publish(exchange, 'pong');
}
});
});
conn.end();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment