-
Notifications
You must be signed in to change notification settings - Fork 0
/
t3-topics-consumer.js
34 lines (25 loc) · 1.03 KB
/
t3-topics-consumer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var amqp = require('amqplib');
var key = process.argv[2] || 'log.info';
amqp.connect('amqp://localhost').then(function (conn) {
process.once('SIGINT', function() { conn.close(); });
return conn.createChannel().then(function(ch) {
var ex = 'topic_logs';
var ok = ch.assertExchange(ex, 'topic', {durable: false});
ok = ok.then(function() {
return ch.assertQueue('', {exclusive: true});
});
ok = ok.then(function(qok) {
var queue = qok.queue;
return ch.bindQueue(queue, ex, key).then(function() { return queue; });
});
ok = ok.then(function(queue) {
return ch.consume(queue, logMessage, {noAck: true});
});
return ok.then(function() {
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
});
function logMessage(msg) {
console.log(" [x] %s:'%s'", msg.fields.routingKey, msg.content.toString());
}
});
}).then(null, console.warn);