forked from jwalton/node-amqp-connection-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub-publisher.js
33 lines (28 loc) · 1.04 KB
/
pubsub-publisher.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
const amqp = require('..');
const {wait} = require('../lib/helpers');
const EXCHANGE_NAME = 'amqp-connection-manager-sample2-ex';
// Create a connetion manager
const connection = amqp.connect(['amqp://localhost']);
connection.on('connect', () => console.log('Connected!'));
connection.on('disconnect', err => console.log('Disconnected.', err.stack));
// Create a channel wrapper
const channelWrapper = connection.createChannel({
json: true,
setup: channel => channel.assertExchange(EXCHANGE_NAME, 'topic')
});
// Send messages until someone hits CTRL-C or something goes wrong...
function sendMessage() {
channelWrapper.publish(EXCHANGE_NAME, "test", {time: Date.now()}, { contentType: 'application/json', persistent: true })
.then(function() {
console.log("Message sent");
return wait(1000);
})
.then(() => sendMessage())
.catch(err => {
console.log("Message was rejected:", err.stack);
channelWrapper.close();
connection.close();
});
};
console.log("Sending messages...");
sendMessage();