-
Notifications
You must be signed in to change notification settings - Fork 1
/
pub.js
44 lines (37 loc) · 921 Bytes
/
pub.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
var _ = require('lodash');
var fs = require('fs');
var NATS = require('nats');
var nc = NATS.connect({
verbose: true,
json: true,
name: 'publisher',
// token: 'GX5YF3L7DL90SP7PLFOUP9',
user: 'joe',
pass: 'GX5YF3L7DL90SP7PLFOUP9',
tls: {
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem'),
rejectUnauthorized: false
}
});
nc.on('connect', () => {
console.log('Connected to NATS');
});
setInterval(() => {
let subject = 'dashboard';
let tab = _.random(1,5);
console.log(`Sending message to: ${subject}.${tab}`);
nc.publish(`${subject}.${tab}`, { tab: tab, epoch: new Date().toISOString() });
}, 2000);
nc.on('disconnect', function() {
console.log('disconnect');
});
nc.on('reconnecting', function() {
console.log('reconnecting');
});
nc.on('reconnect', function(nc) {
console.log('reconnect');
});
nc.on('close', function() {
console.log('close');
});