Skip to content

Commit

Permalink
[feature] Add an option to disable channel multiplexing (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne authored Nov 28, 2016
1 parent 84261b2 commit 298dff2
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 466 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The following options are allowed:
- `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`)

If you decide to supply `pubClient` and `subClient`, make sure you use
[node_redis](https://github.com/mranney/node_redis) as a client or one
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function adapter(uri, opts){
var prefix = opts.key || 'socket.io';
var subEvent = opts.subEvent || 'message';
var requestsTimeout = opts.requestsTimeout || 1000;
var withChannelMultiplexing = false !== opts.withChannelMultiplexing;

// init clients if needed
function createClient(redis_opts) {
Expand Down Expand Up @@ -79,6 +80,7 @@ function adapter(uri, opts){
this.uid = uid;
this.prefix = prefix;
this.requestsTimeout = requestsTimeout;
this.withChannelMultiplexing = withChannelMultiplexing;

this.channel = prefix + '#' + nsp.name + '#';
this.requestChannel = prefix + '-request#' + this.nsp.name + '#';
Expand Down Expand Up @@ -279,7 +281,7 @@ function adapter(uri, opts){
if (!(remote || (opts && opts.flags && opts.flags.local))) {
var self = this;
var msg = msgpack.encode([uid, packet, opts]);
if (opts.rooms) {
if (self.withChannelMultiplexing && opts.rooms) {
opts.rooms.forEach(function(room) {
var chnRoom = self.channel + room + '#';
pub.publish(chnRoom, msg);
Expand All @@ -304,6 +306,11 @@ function adapter(uri, opts){
debug('adding %s to %s ', id, room);
var self = this;
Adapter.prototype.add.call(this, id, room);

if (!this.withChannelMultiplexing) {
if (fn) fn(null);
return;
}
var channel = this.channel + room + '#';
sub.subscribe(channel, function(err){
if (err) {
Expand Down Expand Up @@ -331,7 +338,7 @@ function adapter(uri, opts){
var hasRoom = this.rooms.hasOwnProperty(room);
Adapter.prototype.del.call(this, id, room);

if (hasRoom && !this.rooms[room]) {
if (this.withChannelMultiplexing && hasRoom && !this.rooms[room]) {
var channel = this.channel + room + '#';
sub.unsubscribe(channel, function(err){
if (err) {
Expand Down
Loading

0 comments on commit 298dff2

Please sign in to comment.