-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.js
49 lines (41 loc) · 1.25 KB
/
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
const amqp = require('amqplib');
const amqpServerURL = 'amqp://localhost:5672';
const exchangeName = 'broadcast_logs';
const init = async () => {
try {
const connection = await amqp.connect(amqpServerURL);
const channel = await connection.createChannel();
await channel.assertExchange(exchangeName, 'fanout', {
durable: false,
});
/**
* We passed queue name as an empty string, that will create a non-durable queue with random queue name generated by RabbitMQ
* exclusive: true (used by only one connection and the queue will be deleted when that connection closes)
*/
const tempQueue = await channel.assertQueue('', {
exclusive: true,
});
console.log(
`Waiting for messages in ${tempQueue.queue}. To exit press CTRL+C`
);
/**
* bindQueue(queue, source, pattern, [args])
* Assert a routing path from an exchange to a queue.
*/
await channel.bindQueue(tempQueue.queue, exchangeName, '');
channel.consume(
tempQueue.queue,
(msg) => {
if (msg.content) {
console.log(`Received job with message ${msg.content.toString()}`);
}
},
{
noAck: true,
}
);
} catch (error) {
console.error(error);
}
};
init();