-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
44 lines (38 loc) · 1.07 KB
/
server.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
const { v4: uuidv4 } = require('uuid');
const RABBITMQ = 'amqp://guest:guest@localhost:5672';
const open = require('amqplib').connect(RABBITMQ);
const q = 'example';
// Consumer
open
.then(function (conn) {
console.log(`[ ${new Date()} ] Server started`);
return conn.createChannel();
})
.then(function (ch) {
return ch.assertQueue(q).then(function (ok) {
return ch.consume(q, function (msg) {
console.log(
`[ ${new Date()} ] Message received: ${JSON.stringify(
JSON.parse(msg.content.toString('utf8')),
)}`,
);
if (msg !== null) {
const response = {
uuid: uuidv4(),
};
console.log(
`[ ${new Date()} ] Message sent: ${JSON.stringify(response)}`,
);
ch.sendToQueue(
msg.properties.replyTo,
Buffer.from(JSON.stringify(response)),
{
correlationId: msg.properties.correlationId,
},
);
ch.ack(msg);
}
});
});
})
.catch(console.warn);