Skip to content

Commit 298dff2

Browse files
[feature] Add an option to disable channel multiplexing (#140)
1 parent 84261b2 commit 298dff2

File tree

4 files changed

+262
-466
lines changed

4 files changed

+262
-466
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The following options are allowed:
3636
- `pubClient`: optional, the redis client to publish events on
3737
- `subClient`: optional, the redis client to subscribe to events on
3838
- `requestsTimeout`: optional, after this timeout the adapter will stop waiting from responses to request (`1000ms`)
39+
- `withChannelMultiplexing`: optional, whether channel multiplexing is enabled (a new subscription will be trigggered for each room) (`true`)
3940

4041
If you decide to supply `pubClient` and `subClient`, make sure you use
4142
[node_redis](https://github.com/mranney/node_redis) as a client or one

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function adapter(uri, opts){
4949
var prefix = opts.key || 'socket.io';
5050
var subEvent = opts.subEvent || 'message';
5151
var requestsTimeout = opts.requestsTimeout || 1000;
52+
var withChannelMultiplexing = false !== opts.withChannelMultiplexing;
5253

5354
// init clients if needed
5455
function createClient(redis_opts) {
@@ -79,6 +80,7 @@ function adapter(uri, opts){
7980
this.uid = uid;
8081
this.prefix = prefix;
8182
this.requestsTimeout = requestsTimeout;
83+
this.withChannelMultiplexing = withChannelMultiplexing;
8284

8385
this.channel = prefix + '#' + nsp.name + '#';
8486
this.requestChannel = prefix + '-request#' + this.nsp.name + '#';
@@ -279,7 +281,7 @@ function adapter(uri, opts){
279281
if (!(remote || (opts && opts.flags && opts.flags.local))) {
280282
var self = this;
281283
var msg = msgpack.encode([uid, packet, opts]);
282-
if (opts.rooms) {
284+
if (self.withChannelMultiplexing && opts.rooms) {
283285
opts.rooms.forEach(function(room) {
284286
var chnRoom = self.channel + room + '#';
285287
pub.publish(chnRoom, msg);
@@ -304,6 +306,11 @@ function adapter(uri, opts){
304306
debug('adding %s to %s ', id, room);
305307
var self = this;
306308
Adapter.prototype.add.call(this, id, room);
309+
310+
if (!this.withChannelMultiplexing) {
311+
if (fn) fn(null);
312+
return;
313+
}
307314
var channel = this.channel + room + '#';
308315
sub.subscribe(channel, function(err){
309316
if (err) {
@@ -331,7 +338,7 @@ function adapter(uri, opts){
331338
var hasRoom = this.rooms.hasOwnProperty(room);
332339
Adapter.prototype.del.call(this, id, room);
333340

334-
if (hasRoom && !this.rooms[room]) {
341+
if (this.withChannelMultiplexing && hasRoom && !this.rooms[room]) {
335342
var channel = this.channel + room + '#';
336343
sub.unsubscribe(channel, function(err){
337344
if (err) {

0 commit comments

Comments
 (0)