-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.js
58 lines (49 loc) · 1.07 KB
/
send.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
50
51
52
53
54
55
56
57
58
var amqp = require('amqplib/callback_api');
const createConnection = () => {
return new Promise((resolve, reject) => {
amqp.connect('amqp://localhost', (err, connection) => {
if (err)
return reject(err)
resolve(connection);
})
})
}
const createChannel = () => {
return new Promise(async (resolve, reject) => {
try
{
let connection = await createConnection();
connection.createChannel((err, channel) => {
if (err)
return reject(err)
resolve(channel);
})
}
catch (err)
{
reject(err);
}
})
}
/**
* Accepts a queue name string and a javascript object as a message to send
* @param {string} queue
* @param {object} message
*/
const sendMessage = async (queue, message) => {
try
{
let channel = await createChannel();
channel.assertQueue(queue, {durable: true});
channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)));
}
catch (err)
{
let e = new Error(err);
e.name = 'sendMessage'
throw e;
}
}
module.exports = {
sendMessage
}