-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsumer.js
More file actions
37 lines (28 loc) · 1.03 KB
/
consumer.js
File metadata and controls
37 lines (28 loc) · 1.03 KB
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
const amqplib = require('amqplib');
const amqpAddress = process.env.AMQP_URL || 'amqp://localhost:5673';
async function processMessage(msg) {
console.log(msg.content.toString(), 'Sending Email from API');
}
(async () => {
const connection = await amqplib.connect(amqpAddress, "heartbeat=60");
const channel = await connection.createChannel();
// Number of messages retrieved at a time
channel.prefetch(10);
const queue = 'user.sign_up_data';
process.once('SIGINT', async () => {
console.log('Close the channel and connection before exiting the process');
await channel.close();
await connection.close();
process.exit(0);
});
await channel.assertQueue(queue, { durable: true });
await channel.consume(queue, async (message) => {
console.log('Processing Messages');
await processMessage(message);
await channel.ack(message);
}, {
noAck: false,
consumerTag: 'email_consumer'
});
console.log("Waiting for Messages");
})();