-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sender.js
39 lines (36 loc) · 1.11 KB
/
sender.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
const amqp = require('amqplib');
const { v4: uuidv4 } = require('uuid');
// setup queue name
const queueName = 'test-queue';
/**
* Send message
*/
async function send() {
// connect to RabbitMQ
const connection = await amqp.connect(process.env.RABBITMQ_HOST || 'amqp://localhost');
// create a channel
const channel = await connection.createChannel();
// create/update a queue to make sure the queue is exist
await channel.assertQueue(queueName, {
durable: true,
});
// generate correlation id, basically correlation id used to know if the message is still related with another message
const correlationId = uuidv4();
// send 10 messages and generate message id for each messages
for (let i = 1; i <= 10; i++) {
const buff = Buffer.from(JSON.stringify({
test: `Hello World ${i}!!`
}), 'utf-8');
const result = channel.sendToQueue(queueName, buff, {
persistent: true,
messageId: uuidv4(),
correlationId: correlationId,
});
console.log(result);
}
// close the channel
await channel.close();
// close the connection
await connection.close();
}
send();