Permalink
Browse files

node: second tutorial

  • Loading branch information...
1 parent 4fbf330 commit d45d9398822759ecbd870822225b12d9d80e12b2 @majek majek committed Apr 17, 2012
Showing with 25 additions and 4 deletions.
  1. +4 −3 javascript-nodejs/receive.js
  2. +1 −1 javascript-nodejs/send.js
  3. +20 −0 javascript-nodejs/worker.js
@@ -1,13 +1,14 @@
var amqp = require('amqp');
-var amqp_hacks = require('./amqp-hacks');
-var connection = amqp.createConnection({ host: 'localhost' });
+var connection = amqp.createConnection({host: 'localhost'});
connection.on('ready', function(){
connection.queue('hello', {autoDelete: false}, function(queue){
+
+ console.log(' [*] Waiting for messages. To exit press CTRL+C')
+
queue.subscribe(function(msg){
console.log(" [x] Received %s", msg.data.toString('utf-8'));
});
});
- console.log(' [*] Waiting for messages. To exit press CTRL+C')
});
@@ -1,7 +1,7 @@
var amqp = require('amqp');
var amqp_hacks = require('./amqp-hacks');
-var connection = amqp.createConnection({ host: 'localhost' });
+var connection = amqp.createConnection({host: 'localhost'});
connection.on('ready', function(){
connection.publish('hello', 'Hello World!');
@@ -0,0 +1,20 @@
+var amqp = require('amqp');
+
+var connection = amqp.createConnection({host: 'localhost'});
+
+connection.on('ready', function(){
+ connection.queue('task_queue', {autoDelete: false,
+ durable: true}, function(queue){
+
+ console.log(' [*] Waiting for messages. To exit press CTRL+C');
+
+ queue.subscribe({ack: true, prefetchCount: 1}, function(msg){
+ var body = msg.data.toString('utf-8');
+ console.log(" [x] Received %s", body);
+ setTimeout(function(){
+ console.log(" [x] Done");
+ queue.shift(); // basic_ack equivalent
+ }, (body.split('.').length - 1) * 1000);
+ });
+ });
+});

0 comments on commit d45d939

Please sign in to comment.