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

Skip to content

Commit 5b977da

Browse files
committed
Publish/Subscribe tutorial receive_logs.js
1 parent 6d6a421 commit 5b977da

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

javascript-nodejs/src/receive_logs.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
3+
var amqp = require('amqplib');
4+
5+
var conn = amqp.connect('amqp://localhost')
6+
7+
conn.then(createChannel).then(null, console.warn);
8+
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');
26+
});
27+
28+
function logMessage(msg) {
29+
console.log(" [x] '%s'", msg.content.toString());
30+
}
31+
});
32+
}

0 commit comments

Comments
 (0)
0