node: third tutorial

This commit is contained in:
Marek Majkowski 2012-04-17 17:04:41 +01:00
parent d45d939882
commit d3b0ab2ebe
3 changed files with 38 additions and 0 deletions

View File

@ -20,3 +20,7 @@ pull the dependency from `npm` run:
node send.js
node receive.js
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html):
node receive_logs.js
node emit_log.js "info: This is the log message"

View File

@ -0,0 +1,16 @@
var amqp = require('amqp');
var amqp_hacks = require('./amqp-hacks');
var connection = amqp.createConnection({host: 'localhost'});
var message = process.argv.slice(2).join(' ') || 'Hello World!';
connection.on('ready', function(){
connection.exchange('logs', {type: 'fanout',
autoDelete: false}, function(exchange){
exchange.publish('', message);
console.log(" [x] Sent %s", message);
amqp_hacks.safeEndConnection(connection);
});
});

View File

@ -0,0 +1,18 @@
var amqp = require('amqp');
var connection = amqp.createConnection({host: 'localhost'});
connection.on('ready', function(){
connection.exchange('logs', {type: 'fanout',
autoDelete: false}, function(exchange){
connection.queue('tmp-' + Math.random(), {exclusive: true},
function(queue){
queue.bind('logs', '');
console.log(' [*] Waiting for logs. To exit press CTRL+C')
queue.subscribe(function(msg){
console.log(" [x] %s", msg.data.toString('utf-8'));
});
})
});
});