Skip to content

Add support for custom data when returning a list of all socket clients #154

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -62,6 +62,12 @@ that a regular `Adapter` does not

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

### RedisAdapter#setCustomClientHook(fn:Function)

Sets a custom hook on the RedisAdapter#clients call, so that its callback
provides custom data related to a client in addition to the array of client IDs.
The input function takes a single argument, clientID.

## Client error handling

Access the `pubClient` and `subClient` properties of the
Expand Down
53 changes: 41 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,20 @@ function adapter(uri, opts){
return;
}

// construct client custom data from the custom hook
var clientsData = [];
for(var i=0; i<clients.length; i++) {
clientsData[i] = {
clientId: clients[i]
};
var clientInfo = Redis.customClientHook(clients[i]);
if(clientInfo) {
clientsData[i].clientInfo = clientInfo;
}
}

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

pub.publish(responseChn, response);
});
Expand Down Expand Up @@ -318,7 +328,7 @@ function adapter(uri, opts){
numsub = numsub[1];

var msg_count = 0;
var clients = {};
var clientsData = {};

subJson.subscribe(responseChn, function(err) {
if (err) {
Expand All @@ -334,7 +344,7 @@ function adapter(uri, opts){

/*If there is no response for 1 second, return result;*/
var timeout = setTimeout(function() {
if (fn) process.nextTick(fn.bind(null, null, Object.keys(clients)));
if (fn) process.nextTick(fn.bind(null, null, Object.keys(clientsData), clientsData));
}, self.clientsTimeout);

subJson.on(subEvent, function onEvent(channel, msg) {
Expand All @@ -344,12 +354,8 @@ function adapter(uri, opts){
}

var response = JSON.parse(msg);

//Ignore if response does not contain 'clients' key
if(!response.clients || !Array.isArray(response.clients)) return;

for(var i = 0; i < response.clients.length; i++){
clients[response.clients[i]] = true;
for(var i = 0; i < response.length; i++){
clientsData[response[i].clientId] = response[i].clientInfo;
}

msg_count++;
Expand All @@ -358,7 +364,7 @@ function adapter(uri, opts){
subJson.unsubscribe(responseChn);
subJson.removeListener(subEvent, onEvent);

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

Expand All @@ -370,6 +376,29 @@ function adapter(uri, opts){

};

/**
* Setter for the custom hook function.
*
* @param {Function} the function to be set as customClientHook
* @api public
*/

Redis.setCustomClientHook = function(fn) {
Redis.customClientHook = fn;
}

/**
* A custom hook for redis consumer to overwrite to return custom data
* of a socket client.
*
* @param {String} client id
* @api private
*/

Redis.customClientHook = function() {
return null;
}

Redis.uid = uid;
Redis.pubClient = pub;
Redis.subClient = sub;
Expand Down