Skip to content

Commit d45d939

Browse files
committed
node: second tutorial
1 parent 4fbf330 commit d45d939

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

javascript-nodejs/receive.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
var amqp = require('amqp');
2-
var amqp_hacks = require('./amqp-hacks');
32

4-
var connection = amqp.createConnection({ host: 'localhost' });
3+
var connection = amqp.createConnection({host: 'localhost'});
54

65
connection.on('ready', function(){
76
connection.queue('hello', {autoDelete: false}, function(queue){
7+
8+
console.log(' [*] Waiting for messages. To exit press CTRL+C')
9+
810
queue.subscribe(function(msg){
911
console.log(" [x] Received %s", msg.data.toString('utf-8'));
1012
});
1113
});
12-
console.log(' [*] Waiting for messages. To exit press CTRL+C')
1314
});

javascript-nodejs/send.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var amqp = require('amqp');
22
var amqp_hacks = require('./amqp-hacks');
33

4-
var connection = amqp.createConnection({ host: 'localhost' });
4+
var connection = amqp.createConnection({host: 'localhost'});
55

66
connection.on('ready', function(){
77
connection.publish('hello', 'Hello World!');

javascript-nodejs/worker.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var amqp = require('amqp');
2+
3+
var connection = amqp.createConnection({host: 'localhost'});
4+
5+
connection.on('ready', function(){
6+
connection.queue('task_queue', {autoDelete: false,
7+
durable: true}, function(queue){
8+
9+
console.log(' [*] Waiting for messages. To exit press CTRL+C');
10+
11+
queue.subscribe({ack: true, prefetchCount: 1}, function(msg){
12+
var body = msg.data.toString('utf-8');
13+
console.log(" [x] Received %s", body);
14+
setTimeout(function(){
15+
console.log(" [x] Done");
16+
queue.shift(); // basic_ack equivalent
17+
}, (body.split('.').length - 1) * 1000);
18+
});
19+
});
20+
});

0 commit comments

Comments
 (0)