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