Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all clients in a room across all nodes #109

Merged
merged 6 commits into from
Sep 24, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The following options are allowed:
- `subEvent`: optional, the redis client event name to subscribe to (`message`)
- `pubClient`: optional, the redis client to publish events on
- `subClient`: optional, the redis client to subscribe to events on
- `clientsTimeout`: optional, after this timeout the adapter will stop waiting from responses to `clients` request (`1000ms`)

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 All @@ -55,6 +56,11 @@ that a regular `Adapter` does not
- `prefix`
- `pubClient`
- `subClient`
- `clientsTimeout`

### RedisAdapter#clients(rooms:Array, fn:Function)

Returns the list of client IDs connected to `rooms` across all nodes. See [Namespace#clients(fn:Function)](https://github.com/socketio/socket.io#namespaceclientsfnfunction)

## Client error handling

Expand Down
128 changes: 128 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ function adapter(uri, opts){
// opts
var pub = opts.pubClient;
var sub = opts.subClient;

var prefix = opts.key || 'socket.io';
var subEvent = opts.subEvent || 'message';
var clientsTimeout = opts.clientsTimeout || 1000;

// init clients if needed
function createClient(redis_opts) {
Expand All @@ -52,6 +54,8 @@ function adapter(uri, opts){

if (!pub) pub = createClient();
if (!sub) sub = createClient({ return_buffers: true });

var subJson = sub.duplicate({ return_buffers: false });

// this server's key
var uid = uid2(6);
Expand All @@ -68,7 +72,11 @@ function adapter(uri, opts){

this.uid = uid;
this.prefix = prefix;
this.clientsTimeout = clientsTimeout;

this.channel = prefix + '#' + nsp.name + '#';
this.syncChannel = prefix + '-sync#request#' + this.nsp.name + '#';

if (String.prototype.startsWith) {
this.channelMatches = function (messageChannel, subscribedChannel) {
return messageChannel.startsWith(subscribedChannel);
Expand All @@ -82,10 +90,17 @@ function adapter(uri, opts){
this.subClient = sub;

var self = this;

sub.subscribe(this.channel, function(err){
if (err) self.emit('error', err);
});

subJson.subscribe(this.syncChannel, function(err){
if (err) self.emit('error', err);
});

sub.on(subEvent, this.onmessage.bind(this));
subJson.on(subEvent, this.onclients.bind(this));
}

/**
Expand Down Expand Up @@ -124,6 +139,41 @@ function adapter(uri, opts){
this.broadcast.apply(this, args);
};

/**
* Called with a subscription message on sync
*
* @api private
*/

Redis.prototype.onclients = function(channel, msg){

if (!this.channelMatches(channel.toString(), this.syncChannel)) {
return debug('ignore different channel');
}

try {
var decoded = JSON.parse(msg);
} catch(err){
self.emit('error', err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: self seems undefined here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

return;
}

Adapter.prototype.clients.call(this, decoded.rooms, function(err, clients){
if(err){
self.emit('error', err);
return;
}

var responseChn = prefix + '-sync#response#' + decoded.transaction;
var response = JSON.stringify({
clients : clients
});

pub.publish(responseChn, response);
});

};

/**
* Broadcasts a packet.
*
Expand Down Expand Up @@ -236,10 +286,88 @@ function adapter(uri, opts){
});
};

/**
* Gets a list of clients by sid.
*
* @param {Array} explicit set of rooms to check.
* @api public
*/

Redis.prototype.clients = function(rooms, fn){
if ('function' == typeof rooms){
fn = rooms;
rooms = null;
}

rooms = rooms || [];

var self = this;

var transaction = uid2(6);
var responseChn = prefix + '-sync#response#' + transaction;

pub.send_command('pubsub', ['numsub', self.syncChannel], function(err, numsub){
if (err) {
self.emit('error', err);
if (fn) fn(err);
return;
}

numsub = numsub[1];

var msg_count = 0;
var clients = {};

subJson.on("subscribe", function subscribed(channel, count) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for double quotes here?


var request = JSON.stringify({
transaction : transaction,
rooms : rooms
});

/*If there is no response for 5 seconds, return result;*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 seconds?

var timeout = setTimeout(function() {
if (fn) process.nextTick(fn.bind(null, null, Object.keys(clients)));
}, self.clientsTimeout);

subJson.on(subEvent, function onEvent(channel, msg) {

if (!self.channelMatches(channel.toString(), responseChn)) {
return debug('ignore different channel');
}

var response = JSON.parse(msg);

for(var i = 0; i < response.clients.length; i++){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you maybe add a test for the existence of the clients property?

clients[response.clients[i]] = true;
}

msg_count++;
if(msg_count == numsub){
clearTimeout(timeout);
subJson.unsubscribe(responseChn);
subJson.removeListener('subscribe', subscribed);
subJson.removeListener(subEvent, onEvent);

if (fn) process.nextTick(fn.bind(null, null, Object.keys(clients)));
}
});

pub.publish(self.syncChannel, request);

});

subJson.subscribe(responseChn);

});

};

Redis.uid = uid;
Redis.pubClient = pub;
Redis.subClient = sub;
Redis.prefix = prefix;
Redis.clientsTimeout = clientsTimeout;

return Redis;

Expand Down
44 changes: 44 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,50 @@ describe('socket.io-redis', function(){
});
});

it('returns clients in the same room', function(done){
create(function(server1, client1){
create(function(server2, client2){
create(function(server3, client3){

var ready = 0;

server1.on('connection', function(c1){
c1.join('woot');
ready++;;
if(ready === 3){
test();
}
});

server2.on('connection', function(c1){
c1.join('woot');
ready++;;
if(ready === 3){
test();
}
});

server3.on('connection', function(c3){
ready++;;
if(ready === 3){
test();
}
});

function test(){
setTimeout(function(){
server1.adapter.clients(['woot'], function(err, clients){
expect(clients.length).to.eql(2);
done();
})
}, 100);
}

});
});
});
});

// create a pair of socket.io server+client
function create(nsp, fn){
var srv = http();
Expand Down