-
Notifications
You must be signed in to change notification settings - Fork 0
/
Consumer.js
47 lines (37 loc) · 1.29 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
import { AMQPClient } from '@cloudamqp/amqp-client'
import {} from 'dotenv/config'
const lavinmqUrl = process.env.CLOUDAMQP_URL
const acknoledgement = async(channel, msg)=>{
console.log(" [✅] Received " + msg)
setTimeout(() => {
console.log("[✅] Image resized!");
channel.close()
//need to ack
}, 5000);
}
async function startConsumer() {
//Setup a connection to the RabbitMQ server
const connection = new AMQPClient(lavinmqUrl)
await connection.connect()
const channel = await connection.channel()
console.log("[✅] Connection over channel established")
console.log("[❎] Waiting for messages. To exit press CTRL+C ")
const q = await channel.queue('test6', {durable: true})
let counter = 0;
await q.subscribe({noAck: false}, async (msg) => {
try {
console.log(`[📤] Message received (${++counter})`, msg.bodyToString())
} catch (error) {
console.error(error)
}
})
await acknoledgement(channel, `${counter} messages`)
//When the process is terminated, close the connection
process.on('SIGINT', () => {
channel.close()
connection.close()
console.log("[❎] Connection closed")
process.exit(0)
});
}
startConsumer().catch(console.error);