Skip to content

Instantly share code, notes, and snippets.

@nemtsov
Created July 2, 2012 08:10
Show Gist options
  • Save nemtsov/3031809 to your computer and use it in GitHub Desktop.
Save nemtsov/3031809 to your computer and use it in GitHub Desktop.
amqp pub-sub
var cluster = require('cluster')
, amqp = require('amqp');
if (cluster.isMaster) {
for (var i = 0; i < 5; i++) cluster.fork();
startPublisher();
} else {
startSubscriber();
}
function startPublisher() {
var conn = amqp.createConnection({host: 'localhost'});
conn.on('ready', function () {
var exc = conn.exchange('logs', {type: 'fanout'});
exc.publish('', 'before');
var cnt = 0;
setInterval(function () {
console.log('PUBLISHING');
exc.publish('', 'hello world: ' + cnt++);
}, 50);
});
}
function startSubscriber() {
var conn = amqp.createConnection({host: 'localhost'});
conn.on('ready', function () {
var exc = conn.exchange('logs', {type: 'fanout'});
conn.queue('', {exclusive: true}, function (queue) {
queue.bind(exc, '');
queue.subscribe(function (message) {
console.log(message.data.toString());
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment