socket.io-redis
Advanced tools
Comparing version 4.0.1 to 5.0.0
5.0.0 / 2017-05-11 | ||
=================== | ||
* [perf] Use notepack instead of msgpack-lite (#218) | ||
* [perf] Use pattern matching at the namespace level (#217) | ||
4.0.1 / 2017-05-11 | ||
@@ -3,0 +9,0 @@ =================== |
148
index.js
@@ -8,6 +8,5 @@ | ||
var redis = require('redis').createClient; | ||
var msgpack = require('msgpack-lite'); | ||
var msgpack = require('notepack.io'); | ||
var Adapter = require('socket.io-adapter'); | ||
var debug = require('debug')('socket.io-redis'); | ||
var async = require('async'); | ||
@@ -54,7 +53,4 @@ /** | ||
var sub = opts.subClient; | ||
var prefix = opts.key || 'socket.io'; | ||
var subEvent = opts.subEvent || 'messageBuffer'; | ||
var requestsTimeout = opts.requestsTimeout || 1000; | ||
var withChannelMultiplexing = false !== opts.withChannelMultiplexing; | ||
@@ -90,3 +86,2 @@ // init clients if needed | ||
this.requestsTimeout = requestsTimeout; | ||
this.withChannelMultiplexing = withChannelMultiplexing; | ||
@@ -113,8 +108,14 @@ this.channel = prefix + '#' + nsp.name + '#'; | ||
sub.subscribe([this.channel, this.requestChannel, this.responseChannel], function(err){ | ||
sub.psubscribe(this.channel + '*', function(err){ | ||
if (err) self.emit('error', err); | ||
}); | ||
sub.on(subEvent, this.onmessage.bind(this)); | ||
sub.on('pmessageBuffer', this.onmessage.bind(this)); | ||
sub.subscribe([this.requestChannel, this.responseChannel], function(err){ | ||
if (err) self.emit('error', err); | ||
}); | ||
sub.on('messageBuffer', this.onrequest.bind(this)); | ||
function onError(err) { | ||
@@ -139,17 +140,18 @@ self.emit('error', err); | ||
Redis.prototype.onmessage = function(channel, msg){ | ||
Redis.prototype.onmessage = function(pattern, channel, msg){ | ||
channel = channel.toString(); | ||
if (this.channelMatches(channel, this.requestChannel)) { | ||
return this.onrequest(channel, msg); | ||
} else if (this.channelMatches(channel, this.responseChannel)) { | ||
return this.onresponse(channel, msg); | ||
} else if (!this.channelMatches(channel, this.channel)) { | ||
if (!this.channelMatches(channel, this.channel)) { | ||
return debug('ignore different channel'); | ||
} | ||
var room = channel.substring(this.channel.length); | ||
if (room !== '' && !this.rooms.hasOwnProperty(room)) { | ||
return debug('ignore unknown room %s', room); | ||
} | ||
var args = msgpack.decode(msg); | ||
var packet; | ||
if (uid == args.shift()) return debug('ignore same uid'); | ||
if (uid === args.shift()) return debug('ignore same uid'); | ||
@@ -178,2 +180,10 @@ packet = args[0]; | ||
Redis.prototype.onrequest = function(channel, msg){ | ||
channel = channel.toString(); | ||
if (this.channelMatches(channel, this.responseChannel)) { | ||
return this.onresponse(channel, msg); | ||
} else if (!this.channelMatches(channel, this.requestChannel)) { | ||
return debug('ignore different channel'); | ||
} | ||
var self = this; | ||
@@ -403,4 +413,4 @@ var request; | ||
var msg = msgpack.encode([uid, packet, opts]); | ||
if (this.withChannelMultiplexing && opts.rooms && opts.rooms.length === 1) { | ||
pub.publish(this.channel + opts.rooms[0] + '#', msg); | ||
if (opts.rooms && opts.rooms.length === 1) { | ||
pub.publish(this.channel + opts.rooms[0], msg); | ||
} else { | ||
@@ -414,103 +424,2 @@ pub.publish(this.channel, msg); | ||
/** | ||
* Subscribe client to room messages. | ||
* | ||
* @param {String} client id | ||
* @param {String} room | ||
* @param {Function} callback (optional) | ||
* @api public | ||
*/ | ||
Redis.prototype.add = function(id, room, fn){ | ||
debug('adding %s to %s ', id, room); | ||
var self = this; | ||
// subscribe only once per room | ||
var alreadyHasRoom = this.rooms.hasOwnProperty(room); | ||
Adapter.prototype.add.call(this, id, room); | ||
if (!this.withChannelMultiplexing || alreadyHasRoom) { | ||
if (fn) fn(null); | ||
return; | ||
} | ||
var channel = this.channel + room + '#'; | ||
function onSubscribe(err) { | ||
if (err) { | ||
self.emit('error', err); | ||
if (fn) fn(err); | ||
return; | ||
} | ||
if (fn) fn(null); | ||
} | ||
sub.subscribe(channel, onSubscribe); | ||
}; | ||
/** | ||
* Unsubscribe client from room messages. | ||
* | ||
* @param {String} session id | ||
* @param {String} room id | ||
* @param {Function} callback (optional) | ||
* @api public | ||
*/ | ||
Redis.prototype.del = function(id, room, fn){ | ||
debug('removing %s from %s', id, room); | ||
var self = this; | ||
var hasRoom = this.rooms.hasOwnProperty(room); | ||
Adapter.prototype.del.call(this, id, room); | ||
if (this.withChannelMultiplexing && hasRoom && !this.rooms[room]) { | ||
var channel = this.channel + room + '#'; | ||
function onUnsubscribe(err) { | ||
if (err) { | ||
self.emit('error', err); | ||
if (fn) fn(err); | ||
return; | ||
} | ||
if (fn) fn(null); | ||
} | ||
sub.unsubscribe(channel, onUnsubscribe); | ||
} else { | ||
if (fn) process.nextTick(fn.bind(null, null)); | ||
} | ||
}; | ||
/** | ||
* Unsubscribe client completely. | ||
* | ||
* @param {String} client id | ||
* @param {Function} callback (optional) | ||
* @api public | ||
*/ | ||
Redis.prototype.delAll = function(id, fn){ | ||
debug('removing %s from all rooms', id); | ||
var self = this; | ||
var rooms = this.sids[id]; | ||
if (!rooms) { | ||
if (fn) process.nextTick(fn.bind(null, null)); | ||
return; | ||
} | ||
async.each(Object.keys(rooms), function(room, next){ | ||
self.del(id, room, next); | ||
}, function(err){ | ||
if (err) { | ||
self.emit('error', err); | ||
if (fn) fn(err); | ||
return; | ||
} | ||
delete self.sids[id]; | ||
if (fn) fn(null); | ||
}); | ||
}; | ||
/** | ||
* Gets a list of clients by sid. | ||
@@ -542,2 +451,3 @@ * | ||
numsub = parseInt(numsub[1], 10); | ||
debug('waiting for %d responses to "clients" request', numsub); | ||
@@ -631,2 +541,3 @@ var request = JSON.stringify({ | ||
numsub = parseInt(numsub[1], 10); | ||
debug('waiting for %d responses to "allRooms" request', numsub); | ||
@@ -807,2 +718,3 @@ var request = JSON.stringify({ | ||
numsub = parseInt(numsub[1], 10); | ||
debug('waiting for %d responses to "customRequest" request', numsub); | ||
@@ -809,0 +721,0 @@ var request = JSON.stringify({ |
{ | ||
"name": "socket.io-redis", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "", | ||
@@ -17,7 +17,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"async": "2.1.4", | ||
"debug": "2.3.3", | ||
"msgpack-lite": "0.1.26", | ||
"notepack.io": "~1.0.1", | ||
"redis": "2.6.3", | ||
"socket.io-adapter": "0.5.0", | ||
"socket.io-adapter": "~1.1.0", | ||
"uid2": "0.0.3" | ||
@@ -29,5 +28,5 @@ }, | ||
"mocha": "3.2.0", | ||
"socket.io": "1.7.x", | ||
"socket.io-client": "1.7.x" | ||
"socket.io": "latest", | ||
"socket.io-client": "latest" | ||
} | ||
} |
@@ -35,7 +35,5 @@ # socket.io-redis | ||
- `port`: port to connect to redis on (`6379`) | ||
- `subEvent`: optional, the redis client event name to subscribe to (`messageBuffer`) | ||
- `pubClient`: optional, the redis client to publish events on | ||
- `subClient`: optional, the redis client to subscribe to events on | ||
- `requestsTimeout`: optional, after this timeout the adapter will stop waiting from responses to request (`1000ms`) | ||
- `withChannelMultiplexing`: optional, whether channel multiplexing is enabled (a new subscription will be trigggered for each room) (`true`) | ||
@@ -42,0 +40,0 @@ If you decide to supply `pubClient` and `subClient`, make sure you use |
5
30871
586
206
+ Addednotepack.io@~1.0.1
+ Addednotepack.io@1.0.1(transitive)
+ Addedsocket.io-adapter@1.1.2(transitive)
- Removedasync@2.1.4
- Removedmsgpack-lite@0.1.26
- Removedasync@2.1.4(transitive)
- Removedcomponent-emitter@1.1.2(transitive)
- Removeddebug@2.2.0(transitive)
- Removedevent-lite@0.1.3(transitive)
- Removedieee754@1.2.1(transitive)
- Removedint64-buffer@0.1.10(transitive)
- Removedisarray@0.0.11.0.0(transitive)
- Removedjson3@3.3.2(transitive)
- Removedlodash@4.17.21(transitive)
- Removedms@0.7.1(transitive)
- Removedmsgpack-lite@0.1.26(transitive)
- Removedsocket.io-adapter@0.5.0(transitive)
- Removedsocket.io-parser@2.3.1(transitive)
Updatedsocket.io-adapter@~1.1.0