-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.js
35 lines (28 loc) · 894 Bytes
/
producer.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
const amqp = require('amqplib');
const amqpServerURL = 'amqp://localhost:5672';
const exchangeName = 'broadcast_logs';
const message = process.argv.slice(2).join(' ') || 'Hello world!';
const init = async () => {
try {
const connection = await amqp.connect(amqpServerURL);
const channel = await connection.createChannel();
/**
* assertExchange(exchange, type, [options])
* Assert an exchange into existence.
*/
await channel.assertExchange(exchangeName, 'fanout', {
durable: false,
});
/**
* publish(exchange, routingKey, content, [options])
* Publish a single message to an exchange.
*/
await channel.publish(exchangeName, '', Buffer.from(message));
console.log(`${message} sent successfully :)`);
await channel.close();
await connection.close();
} catch (error) {
console.error(error);
}
};
init();