-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
44 lines (37 loc) · 1.22 KB
/
worker.js
File metadata and controls
44 lines (37 loc) · 1.22 KB
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
const esClient = require('./libs/elasticsearch');
module.exports.run = function (worker) {
console.log(' >> Worker PID:', process.pid);
var scServer = worker.scServer;
var count = 0;
/*
In here we handle our incoming realtime connections and listen for events.
*/
scServer.on('connection', function (socket) {
// simple_event1
socket.on('simple_event1', function (data) {
count++;
console.log(data);
scServer.exchange.publish('channel_1', `msg: Server ${count}`);
});
// simple_event2
socket.on('simple_event2', (data) => {
console.log(data);
socket.emit('simple_event2', 'msg: Server sent simple_event2 data');
scServer.exchange.publish('channel_2', 'msg: Server sent data to channel_2');
});
// elasticsearch event
socket.on('elasticsearch', () => {
const esData = esClient.searchDocument('pmd_api', 'set_event', '');
// Push es data for client
socket.emit('elasticsearch', esData.hits.hits);
});
var interval = setInterval(function () {
socket.emit('rand', {
rand: Math.floor(Math.random() * 5)
});
}, 1000);
socket.on('disconnect', function () {
clearInterval(interval);
});
});
};