Skip to content

Instantly share code, notes, and snippets.

@revington
Created January 24, 2013 13:40
Show Gist options
  • Save revington/4621708 to your computer and use it in GitHub Desktop.
Save revington/4621708 to your computer and use it in GitHub Desktop.
fanout amqp nodejs npm install amqp debug
var amqp = require('amqp'),
debug = require('debug')('master:' + process.pid), time= 10;
debug('hello');
var connection = amqp.createConnection({
host: 'localhost'
});
var exc;
connection.on('ready', function () {
exc = connection.exchange('my-exchange', {
type: 'fanout'
}, function (exchange) {
debug('Exchange ' + exchange.name + ' is open');
delay(new Date().getTime() + time);
});
});
delay = function (responseAt, next) {
if (responseAt < new Date().getTime()) {
debug('publising a message' + new Date(responseAt));
exc.publish('all', new Date(responseAt));
delay(new Date().getTime() + time);
} else {
process.nextTick(function () {
delay(responseAt, next);
});
}
};
var amqp = require('amqp'),
debug = require('debug')('client:' + process.pid);
debug('hello');
var connection = amqp.createConnection({
host: 'localhost'
});
// Wait for connection to become established.
connection.on('ready', function () {
// Use the default 'amq.topic' exchange
connection.queue('my-queue-' + process.pid, function (q) {
// Catch all messages
q.bind('my-exchange', '#');
// Receive messages
q.subscribe(function (message) {
var command = message.toString('utf-8');
// Print messages to stdout
debug(command);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment