Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

node: third tutorial

  • Loading branch information...
commit d3b0ab2ebec1dccafcd2fe1de1feb333d5180ead 1 parent d45d939
@majek majek authored
View
4 javascript-nodejs/README.md
@@ -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
16 javascript-nodejs/emit_log.js
@@ -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
18 javascript-nodejs/receive_logs.js
@@ -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'));
+ });
+ })
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.