8000 Publish/Subscribe tutorial receive_logs.js · syk-coder/rabbitmq-tutorials@16ecfe9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 16ecfe9

Browse files
committed
Publish/Subscribe tutorial receive_logs.js
1 parent 5b977da commit 16ecfe9

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

javascript-nodejs/src/receive_logs.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@ var amqp = require('amqplib');
44

55
var conn = amqp.connect('amqp://localhost')
66

7-
conn.then(createChannel).then(null, console.warn);
7+
var ch = conn.then(createChannel).then(null, console.warn);
88

9-
function createChannel(conn) {
10-
process.once('SIGINT', function() { conn.close(); });
11-
return conn.createChannel().then(function(ch) {
12-
var ok = ch.assertExchange('logs', 'fanout', {durable: false});
13-
ok = ok.then(function() {
14-
return ch.assertQueue('', {exclusive: true});
15-
});
16-
ok = ok.then(function(qok) {
17-
return ch.bindQueue(qok.queue, 'logs', '').then(function() {
18-
return qok.queue;
19-
});
20-
});
21-
ok = ok.then(function(queue) {
22-
return ch.consume(queue, logMessage, {noAck: true});
23-
});
24-
return ok.then(function() {
25-
console.log(' [*] Waiting for logs. To exit press CTRL+C');
9+
ch.then(function(ch) {
10+
var ok = ch.assertExchange('logs', 'fanout', {durable: false});
11+
ok = ok.then(function() {
12+
return ch.assertQueue('', {exclusive: true});
13+
});
14+
ok = ok.then(function(qok) {
15+
return ch.bindQueue(qok.queue, 'logs', '').then(function() {
16+
return qok.queue;
2617
});
27-
28-
function logMessage(msg) {
29-
console.log(" [x] '%s'", msg.content.toString());
30-
}
3118
});
19+
ok = ok.then(function(queue) {
20+
return ch.consume(queue, logMessage, {noAck: true});
21+
});
22+
return ok.then(function() {
23+
console.log(' [*] Waiting for logs. To exit press CTRL+C');
24+
});
25+
26+
function logMessage(msg) {
27+
console.log(" [x] '%s'", msg.content.toString());
28+
}
29+
});
30+
31+
function createChannel(conn) {
32+
process.once('SIGINT', function() { conn.close(); });
33+
return conn.createChannel();
3234
}

0 commit comments

Comments
 (0)
0