node: third tutorial
This commit is contained in:
parent
d45d939882
commit
d3b0ab2ebe
@ -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"
|
||||
|
16
javascript-nodejs/emit_log.js
Normal file
16
javascript-nodejs/emit_log.js
Normal 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);
|
||||
});
|
||||
});
|
18
javascript-nodejs/receive_logs.js
Normal file
18
javascript-nodejs/receive_logs.js
Normal 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'));
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user