-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgbus.js
76 lines (57 loc) · 2.17 KB
/
msgbus.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// the hub encapsulates functionality to send or receive messages from redis.
var redis = require('redis')
, colors = require('./colors')
, cmd = redis.createClient()
, evt = redis.createClient()
, evtSubscriptions = []
, cmdSubscriptions = [];
module.exports = {
emitCommand: function(command) {
console.log(colors.blue('\nhub -- publishing command ' + command.command + ' to redis:'));
console.log(command);
cmd.publish('commands', JSON.stringify(command));
},
onCommand: function(callback) {
if (cmdSubscriptions.length === 0) {
// subscribe to __commands channel__
cmd.subscribe('commands');
}
cmdSubscriptions.push(callback);
console.log(colors.blue('hub -- command subscribers: ' + cmdSubscriptions.length));
},
emitEvent: function(event) {
console.log(colors.blue('\nhub -- publishing event ' + event.event + ' to redis:'));
console.log(event);
evt.publish('events', JSON.stringify(event));
},
onEvent: function(callback) {
if (evtSubscriptions.length === 0) {
// subscribe to __events channel__
evt.subscribe('events');
}
evtSubscriptions.push(callback);
console.log(colors.blue('hub -- event subscribers: ' + evtSubscriptions.length));
}
};
// listen to events from redis and call each callback from subscribers
evt.on('message', function(channel, message) {
var event = JSON.parse(message);
if (channel === 'events') {
console.log(colors.green('\nhub -- received event ' + event.event + ' from redis:'));
console.log(event);
evtSubscriptions.forEach(function(subscriber){
subscriber(event);
});
}
});
// listen to commands from redis and call each callback from subscribers
cmd.on('message', function(channel, message) {
var command = JSON.parse(message);
if (channel === 'commands') {
console.log(colors.green('\nhub -- received command ' + command.command + ' from redis:'));
console.log(command);
cmdSubscriptions.forEach(function(subscriber){
subscriber(command);
});
}
});