forked from socketio/socket.io-redis-adapter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
177 lines (157 loc) · 5.1 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
'use strict';
/**
* Module dependencies.
*/
const uid2 = require('uid2');
const Adapter = require('socket.io-adapter');
const Emitter = require('events');
const Promise = require('bluebird');
/**
* Returns a redis Adapter class.
*
* @param {{pubClient: Promise.Redis, subClient: Promise.Redis, key: String, subEvent: String, uid2: integer}} opts
* @returns {{new(*=): {onmessage: (function(String, String)), delAll: (function(String, Function)), add: (function(String, String, Function)), del: (function(String, String, Function)), broadcast: (function(Object, Object, Boolean))}}}
* @api public
* @export
*/
function adapter(opts = {}) {
const pub = opts.pubClient;
const sub = opts.subClient;
const prefix = opts.key || 'socket.io';
const subEvent = opts.subEvent || 'message';
if (!pub) throw new Error([" No Pub specified"]);
if (!sub) throw new Error([" No Sub specified"]);
// this server's key
const uid = opts.uid2 ? uid2(opts.uid2) : uid2(6);
/** Create Redis adapter
* @constructor
* @type {{new(*=): {onmessage: (function(String, String)), delAll: (function(String, Function)), add: (function(String, String, Function)), del: (function(String, String, Function)), broadcast: (function(Object, Object, Boolean))}}}
*/
const Redis = class Redis extends Adapter {
constructor(nsp) {
super(nsp);
this.uid = uid;
this.prefix = prefix;
this.channel = prefix + '#' + nsp.name + '#';
this.channelMatches = (messageChannel, subscribedChannel) => messageChannel.startsWith(subscribedChannel);
this.pubClient = pub;
this.subClient = sub;
sub.subscribe(this.channel, (err) => {
if (err) this.emit('error', err);
});
sub.on(subEvent, this.onmessage.bind(this));
}
/** Called with a subscription message
* @param {String} channel
* @param {String} msg
* @api private
*/
onmessage(channel, msg) {
if (!this.channelMatches(channel.toString(), this.channel)) return;
const args = JSON.parse(msg);
if (uid === args.shift()) return;
if (!args[0]) return;
if (args[0].nsp === undefined) args[0].nsp = '/';
if (args[0].nsp !== this.nsp.name) return;
args.push(true);
this.broadcast.apply(this, args);
}
/** Broadcasts a packet.
* @param {Object} packet to emit
* @param {Object} opts
* @param {Boolean} remote whether the packet came from another node
* @api public
*/
broadcast(packet, opts, remote) {
const newPacket = Object.assign({}, packet);
Adapter.prototype.broadcast.call(this, packet, opts);
newPacket.nsp = packet.nsp;
newPacket.type = packet.type;
if (!remote) {
const chn = this.prefix + '#' + newPacket.nsp + '#';
const msg = JSON.stringify([uid, newPacket, opts]);
if (opts.rooms) {
opts.rooms.map((room) => {
const chnRoom = chn + room + '#';
pub.publish(chnRoom, msg);
});
} else {
pub.publish(chn, msg);
}
}
}
/** Subscribe client to room messages.
* @param {String} id client id
* @param {String} room
* @param {Function} fn callback (optional)
* @api public
*/
add(id, room, fn) {
Adapter.prototype.add.call(this, id, room);
const channel = this.prefix + '#' + this.nsp.name + '#' + room + '#';
sub.subscribe(channel, (err) => {
if (err) {
this.emit('error', err);
if (fn) fn(err);
return;
}
if (fn) fn(null);
});
}
/** Unsubscribe client from room messages.
* @param {String} id session id
* @param {String} room id
* @param {Function} fn callback (optional)
* @api public
*/
del(id, room, fn) {
const hasRoom = Object.keys(this.rooms).includes(room);// this.rooms.hasOwnProperty(room);
Adapter.prototype.del.call(this, id, room);
if (hasRoom && !this.rooms[room]) {
const channel = this.prefix + '#' + this.nsp.name + '#' + room + '#';
sub.unsubscribe(channel, (err) => {
if (err) {
this.emit('error', err);
if (fn) fn(err);
return;
}
if (fn) fn(null);
});
} else {
if (fn) process.nextTick(fn.bind(null, null));
}
}
/** Unsubscribe client completely.
* @param {String} id client id
* @param {Function} fn callback (optional)
* @api public
*/
delAll(id, fn) {
const rooms = this.sids[id];
if (!rooms) {
if (fn) process.nextTick(fn.bind(null, null));
return;
}
const room_keys = Object.keys(rooms);
Promise.map(room_keys, (room) => {
this.del(id, room, () => {
delete this.sids[id];
if (fn) fn(null);
});
}, {concurrency: room_keys.length})
.catch((err) => {
this.emit('error', err);
if (fn) fn(err);
});
}
};
Redis.uid = uid;
Redis.pubClient = pub;
Redis.subClient = sub;
Redis.prefix = prefix;
return Redis;
}
/**
* Module exports.
*/
module.exports = adapter;