-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqp-sender.js
47 lines (41 loc) · 1.23 KB
/
amqp-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
40
41
42
43
44
45
46
47
/**
* @file
* Binding Component implementation automatically generated by
* the Social Communication Platform.
*
* Characteristics:
* - Type: AMQP publisher.
* - Social Communication Platform Bus: AMQP.
*/
var amqp = require('amqplib/callback_api');
var Message = require('scb-node-parser/message').Message;
/**
* Sends a message to the Social Communication Platform Bus.
* Protocol: AMQP.
* @param {string} msg - The message to send.
* @param {string} msg - The destination.
*/
exports.post = (msg, exchange) => {
connection = process.env.RABBITMQ;
console.log(connection);
amqp.connect(connection, (err, conn) => {
if (err) {
console.log(err.stack);
} else {
connect(err, conn, msg, exchange);
}
});
};
function connect(err, conn, msg, destination) {
conn.on('error', (err) => {
console.log('An error occurred: ' + err.stack);
});
conn.createChannel((err, ch) => {
ch.assertExchange(destination, 'fanout', {
durable: true
});
message = msg;
ch.publish(destination, '', new Buffer(JSON.stringify(message)));
console.log(" [x] Sent message %s to %s", JSON.stringify(message), destination);
});
}