-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.subscriber.js
42 lines (35 loc) · 1.07 KB
/
rest.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
35
36
37
38
39
40
41
42
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
const dbConfig = require("./config/db.config");
const client = require("amqplib/callback_api");
var q = "reviews";
const url =
"amqp://iiteaaor:[email protected]/iiteaaor";
//callback in case of error
function bail(err) {
console.error(err);
process.exit(1);
}
// Consumer
function consumer(conn) {
var ok = conn.createChannel(on_open);
function on_open(err, ch) {
if (err != null) bail(err);
ch.assertQueue(q);
ch.consume(q, function (msg) {
if (msg !== null) {
const content = msg.content.toString();
const data = JSON.parse(content);
console.log("Review is ", data);
// TODO: after receiving message, call the service
// and update total reviews and avg rating in mongo db for the restaurent
console.log(msg.content.toString());
ch.ack(msg);
}
});
}
}
client.connect(url, function (err, conn) {
if (err != null) bail(err);
console.log("connected , starting consumer");
consumer(conn);
});