-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathamqp_consumer.js
58 lines (40 loc) · 1.47 KB
/
amqp_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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var amqp = require('amqplib'),
util = require('util'),
clients = require('./models/client').repository,
city = require('./models/city');
var CITY_UPDATES_QUEUE = 'city_updated';
var CLIENT_UPDATES_QUEUE = 'client_updated';
// Messaging in Node.JS with RabbitMQ
// https://github.com/squaremo/rabbit.js
amqp.connect('amqp://localhost').then(function(conn) {
process.once('SIGINT', function() {
conn.close();
process.exit(0);
});
return conn.createChannel().then(function(channel) {
var cityOk = channel.assertQueue(CITY_UPDATES_QUEUE, {durable: false});
cityOk = cityOk.then(function(_qok) {
return channel.consume(CITY_UPDATES_QUEUE, function(msg) {
var content = JSON.parse(msg.content)
console.log(" [City] Received:");
console.log(util.inspect(content, {depth: 3}));
city.update(content);
channel.ack(msg);
});
});
cityOk.then(function(_consumeOk) {
console.log(' [*] AMQP Consumer waiting for messages');
});
var clientOk = channel.assertQueue(CLIENT_UPDATES_QUEUE, {durable: false});
cityOk = cityOk.then(function(_qok) {
return channel.consume(CLIENT_UPDATES_QUEUE, function(msg) {
if (!msg.content) return;
var update = JSON.parse(msg.content);
clients.get(update.id, function(err, client) {
if (client) client.update(update);
});
channel.ack(msg);
});
});
});
}).then(null, console.warn);