-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
224 lines (189 loc) · 5.93 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict';
import redis from 'redis';
import { assign,
isUndefined,
isObject } from 'lodash';
import { EventEmitter } from 'events'
export default class Revent extends EventEmitter {
/**
* Redis Event Subscriber
* Subscribe to Redis Keyspace and Keyevent notification
*
* @param config
* @constructor
*/
constructor(config) {
super();
this.queue = {};
const options = assign({
db: 0,
options: {}
messagePattern: null
}, config || {});
const createClient = (port = 6379, host = 'localhost', options = {}) => {
return !isUndefined(options.createClient)
? options.createClient()
: redis.createClient(port, host, options);
};
this.receiver = createClient(options.port, options.host, options.options);
this.publisher = createClient(options.port, options.host, options.options);
this.receiver.select(options.db);
this.publisher.select(options.db);
this.publisher.db_number = this.receiver.db_number = options.db;
if(!isUndefined(options.auth)) {
this.receiver.auth(options.auth);
this.publisher.auth(options.auth);
}
this.config(options.messagePattern);
}
/**
* Init
*
* @method config
* @param {String} pattern RexExp pattern for Redis channel
*/
config(pattern) {
let counter = 0;
const onReady = () => {
counter++;
if(counter === 2) {
this.emit('ready');
this.receiver.on('pmessage', this._onMessage.bind(this, pattern));
}
};
const onError = (err) => {
this.emit('error', err)
};
this.receiver.once("ready", onReady);
this.publisher.once("ready", onReady);
this.receiver.on("error", onError);
this.publisher.on("error", onError);
}
/**
* Subscribe to a channel
*
* @method on
* @param {String} channel The channel to subscribe to, can be a pattern e.g. 'user.*'
* @param {Array} params The array consists of the event name for the subscription and callback
*/
on(event, params) {
if(!Array.isArray(params)) {
return super.on(event, params);
}
const channels = event.split(' '),
cb = params.pop(),
events = params,
DB = this.receiver.db_number;
const subscribe = (channel) => {
let type = (events[0] === 'events') ? '__keyevent@' : '__keyspace@',
key = `${type}${DB}__:${channel}`;
if(!this.queue[key]) this.queue[key] = {};
events.forEach((event) => {
if(!this.queue[key][event])
this.queue[key][event] = [];
this.queue[key][event].push(cb);
});
this.receiver.psubscribe(key);
};
channels.length
? channels.forEach(subscribe)
: subscribe(channels);
return this;
}
/**
* Unsubscribe to a channel
*
* @method off
* @param {String} channel The channel to subscribe to, can be a pattern e.g. 'user.*'
* @param {Array} params The array consists of the event name for the unsubscription and callback
*/
off(event, params) {
if(!Array.isArray(params)) {
return super.off(event, params);
}
const channels = event.split(' '),
cb = params.pop(),
events = params,
DB = this.receiver.db_number;
const unsubscribe = (channel) => {
let type = (!!events.length && events[0] === 'events') ? '__keyevent@' : '__keyspace@',
key = `${type}${DB}__:${channel}`;
if(this.queue[key]) {
this.receiver.punsubscribe(key);
delete this.queue[key];
cb(events, key);
}
};
channels.length
? channels.forEach(unsubscribe)
: unsubscribe(channels);
return this;
}
/**
* Publish an Keyevent
*
* @method send
* @param {String} channel Channel on which to emit the message
* @param {Object|String} message
*/
send(channel, message) {
try {
const data = isObject(message) ? JSON.stringify(message) : message;
this.publisher.publish(`__keyevent@${this.publisher.db_number}__:${channel}`, data);
}
catch (e) {
throw `Publish Error: ${e}`;
}
return this;
}
/*
* Message handler
*
* @method _onMessage
* @param {String} mpattern RegExp for channel match
* @param {String} pattern Channel pattern
* @param {String} channel Channel name
* @param {String} message Channel message from Redis
*/
_onMessage(mpattern, pattern, channel, message) {
let channelID = mpattern ? channel.match(mpattern) : channel.split(":");
(this.queue[pattern][message]
? this.queue[pattern][message]
: this.queue[pattern]['events']
? this.queue[pattern]['events']
: []
).forEach((cb) => {
cb(message, channelID, pattern);
});
}
/**
* Safely connection close
*
* @method quit
*/
close() {
this.receiver.quit();
this.publisher.quit();
}
/**
* Hard connection close
*
* @method end
*/
end() {
this.receiver.end();
this.publisher.end();
}
/**
* Redis commands
*
* @method command
* @return {Object} Return Redis publisher client instance
*/
command() {
return this.publisher;
}
static create(config) {
return new Revent(config);
}
}