forked from jwalton/node-amqp-connection-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub-subscriber.js
34 lines (29 loc) · 1.16 KB
/
pubsub-subscriber.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
const amqp = require('..');
const QUEUE_NAME = 'amqp-connection-manager-sample2'
const EXCHANGE_NAME = 'amqp-connection-manager-sample2-ex';
// Handle an incomming message.
const onMessage = data => {
var message = JSON.parse(data.content.toString());
console.log("subscriber: got message", message);
channelWrapper.ack(data);
}
// Create a connetion manager
const connection = amqp.connect(['amqp://localhost']);
connection.on('connect', () => console.log('Connected!'));
connection.on('disconnect', err => console.log('Disconnected.', err.stack));
// Set up a channel listening for messages in the queue.
var channelWrapper = connection.createChannel({
setup: channel =>
// `channel` here is a regular amqplib `ConfirmChannel`.
return Promise.all([
channel.assertQueue(QUEUE_NAME, { exclusive: true, autoDelete: true }),
channel.assertExchange(EXCHANGE_NAME, 'topic'),
channel.prefetch(1),
channel.bindQueue(QUEUE_NAME, EXCHANGE_NAME, '#'),
channel.consume(QUEUE_NAME, onMessage)
])
});
channelWrapper.waitForConnect()
.then(function() {
console.log("Listening for messages");
});