forked from navaneeth00/socket.io-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
276 lines (232 loc) · 6.96 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Module dependencies.
*/
var uid2 = require('uid2');
var redis = require('redis').createClient;
var msgpack = require('msgpack-js');
var Adapter = require('socket.io-adapter');
var Emitter = require('events').EventEmitter;
var debug = require('debug')('socket.io-redis');
/**
* Module exports.
*/
module.exports = adapter;
/**
* Returns a redis Adapter class.
*
* @param {String} optional, redis uri
* @return {RedisAdapter} adapter
* @api public
*/
function adapter(uri, opts){
opts = opts || {};
// handle options only
if ('object' == typeof uri) {
opts = uri;
uri = null;
}
// handle uri string
if (uri) {
uri = uri.split(':');
opts.host = uri[0];
opts.port = uri[1];
}
// opts
var socket = opts.socket;
var host = opts.host || '127.0.0.1';
var port = Number(opts.port || 6379);
var pub = opts.pubClient;
var sub = opts.subClient;
var data = opts.dataClient;
var prefix = opts.key || 'socket.io';
var node = opts.node || false; // Each instance is given a different name to track the ids written by that instance
// init clients if needed
if (!pub) pub = socket ? redis(socket) : redis(port, host);
if (!sub) sub = socket
? redis(socket, { detect_buffers: true })
: redis(port, host, {detect_buffers: true});
if (!data) data = socket ? redis(socket) : redis(port, host);
// this server's key
var uid = uid2(6);
var key = prefix + '#' + uid;
/**
* Adapter constructor.
*
* @param {String} namespace name
* @api public
*/
var self = this;
function Redis(nsp){
self = this;
Adapter.call(this, nsp);
sub.psubscribe(prefix + '#*', function(err){
if (err) self.emit('error', err);
});
sub.on('pmessage', this.onmessage.bind(this));
this.cleanup(function(err){
if (err) { debug(err) };
debug('Cleaned up stale data from Redis, if any')
});
this.setupExitHandler('SIGTERM', 'SIGINT', 'SIGQUIT');
}
/**
* Inherits from `Adapter`.
*/
Redis.prototype = Object.create(Adapter.prototype);
/**
* Called with a subscription message
*
* @api private
*/
Redis.prototype.onmessage = function(pattern, channel, msg){
var pieces = channel.split('#');
if (uid == pieces.pop()) return debug('ignore same uid');
var args = msgpack.decode(msg);
if (args[0] && args[0].nsp === undefined)
args[0].nsp = '/';
if (!args[0] || args[0].nsp != this.nsp.name) return debug('ignore different namespace');
args.push(true);
this.broadcast.apply(this, args);
};
/**
* Adds a socket from a room.
*
* @param {String} socket id
* @param {String} room name
* @param {Function} callback
* @api public
*/
Redis.prototype.add = function(id, room, fn){
Adapter.prototype.add.call(this, id, room);
var multi = data.multi();
multi.sadd(prefix + '#' + room, id)
.sadd(prefix + '#' + id, room);
if (node) data.sadd(prefix + '#' + node, id); // Add to IDs written in redis by this instance
multi.exec(function(){
if (fn) process.nextTick(fn.bind(null, null));
});
};
/**
* Removes a socket from a room.
*
* @param {String} socket id
* @param {String} room name
* @param {Function} callback
* @api public
*/
Redis.prototype.del = function(id, room, fn){
Adapter.prototype.del.call(this, id, room);
var multi = data.multi();
multi.srem(prefix + '#' + room, id)
.srem(prefix + '#' + id, room);
if (node) multi.srem(prefix + '#' + node, id);
multi.exec(function(){
if (fn) process.nextTick(fn.bind(null, null));
});
};
/**
* Removes a socket from all rooms it's joined.
*
* @param {String} socket id
* @api public
*/
Redis.prototype.delAll = function(id, fn){
Adapter.prototype.delAll.call(this, id);
this.clients(id, function(err, rooms){
if (err) { debug(err) };
var multi = data.multi();
for(var i=0; i<rooms.length; ++i){
multi.srem(prefix + '#' + rooms[i], id);
}
multi.del(prefix + '#' + id);
if (node) multi.srem(prefix + '#' + node, id);
multi.exec(fn);
});
};
/**
* Get all clients in room.
*
* @param {String} room id
* @api public
*/
Redis.prototype.clients = function(room, fn){
data.smembers(prefix + '#' + room, fn);
};
/**
* Broadcasts a packet.
*
* @param {Object} packet to emit
* @param {Object} options
* @param {Boolean} whether the packet came from another node
* @api public
*/
Redis.prototype.broadcast = function(packet, opts, remote){
Adapter.prototype.broadcast.call(this, packet, opts);
if (!remote) pub.publish(key, msgpack.encode([packet, opts]));
};
/**
* Cleans up stale socket IDs from previous runs, when the instance was abruptly stopped
* Requires the option `node` to be set
* @param {function} callback
* @api private
*/
Redis.prototype.cleanup = function(fn) {
var self = this;
var ns = node || prefix; // If multi instance, deletes only stale ids from that instance. Else removes everything
this.clients(ns, function(err, sockIDs) {
var staleIDs = sockIDs;
var errors;
if (staleIDs.length == 0) { return };
function _delStaleSockID (id) {
if (id) {
debug('Removing stale socket id ' + id);
self.delAll(id, function(err, results) {
if (err) {
debug(err);
if (!errors) { errors = [] };
errors.push(err);
}
return _delStaleSockID(staleIDs.shift());
});
} else {
return fn(errors);
}
}
return _delStaleSockID(staleIDs.shift());
});
}
/**
* Set up exit handlers so we can clean up this process's redis data before exiting
* @param {Array} events
* @api private
*/
Redis.prototype.setupExitHandler = function() {
var self = this;
process.stdin.resume(); //so the program will not close instantly
// Copy `arguments` variable to an Array
var events = Array.prototype.slice.call(arguments, 0);
for (var ev = events.length - 1; ev >= 0; ev--) {
// For every event that causes the process to exit, make the cleanup
process.on(events[ev], function() {
debug('Please wait while data stored in redis is cleaned up');
var i;
var multi = data.multi();
var execDone = false;
var roomIds = Object.keys(self.rooms);
var socketIds = Object.keys(self.sids);
for(i=0; i<roomIds.length; ++i){
multi.srem(prefix + '#' + roomIds[i], Object.keys(self.rooms[roomIds[i]]));
}
for(i=0; i<socketIds.length; ++i){
multi.srem(prefix + '#' + socketIds[i], Object.keys(self.sids[socketIds[i]]));
if (node) multi.srem(prefix + '#' + node, Object.keys(self.sids[socketIds[i]]));
}
multi.exec(function(err, replies){
debug('Redis cleanup successful');
process.exit();
});
});
};
};
return Redis;
}