Skip to content

Instantly share code, notes, and snippets.

@mvolkmann
Created May 17, 2012 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvolkmann/2720026 to your computer and use it in GitHub Desktop.
Save mvolkmann/2720026 to your computer and use it in GitHub Desktop.
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