Skip to content

Commit d3b0ab2

Browse files
committed
node: third tutorial
1 parent d45d939 commit d3b0ab2

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

javascript-nodejs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ pull the dependency from `npm` run:
2020
node send.js
2121
node receive.js
2222

23+
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html):
24+
25+
node receive_logs.js
26+
node emit_log.js "info: This is the log message"

javascript-nodejs/emit_log.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var amqp = require('amqp');
2+
var amqp_hacks = require('./amqp-hacks');
3+
4+
var connection = amqp.createConnection({host: 'localhost'});
5+
6+
var message = process.argv.slice(2).join(' ') || 'Hello World!';
7+
8+
connection.on('ready', function(){
9+
connection.exchange('logs', {type: 'fanout',
10+
autoDelete: false}, function(exchange){
11+
exchange.publish('', message);
12+
console.log(" [x] Sent %s", message);
13+
14+
amqp_hacks.safeEndConnection(connection);
15+
});
16+
});

javascript-nodejs/receive_logs.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var amqp = require('amqp');
2+
3+
var connection = amqp.createConnection({host: 'localhost'});
4+
5+
connection.on('ready', function(){
6+
connection.exchange('logs', {type: 'fanout',
7+
autoDelete: false}, function(exchange){
8+
connection.queue('tmp-' + Math.random(), {exclusive: true},
9+
function(queue){
10+
queue.bind('logs', '');
11+
console.log(' [*] Waiting for logs. To exit press CTRL+C')
12+
13+
queue.subscribe(function(msg){
14+
console.log(" [x] %s", msg.data.toString('utf-8'));
15+
});
16+
})
17+
});
18+
});

0 commit comments

Comments
 (0)