I have a socket server, and would like to know if a particular user has one or more connected sockets.
There can be multiple socket servers at different locations for load balancing and scaling purposes, so a client could theoritically have multiple socket connections, each at a different location/instance of the socket server.
On socket connect, I increment a redis key for the user, and on the socket disconnect, I decrement the key.
However, if I restart the socket server(s), either manually, or via forever/supervisor, the decrement counter won't be called, giving an incorrect number for open socket connections for that user.
My question is, it is possible to use the current approach with some tweaking to have an accurate number stored in redis, or is there another approach I should look at using?
EDIT: On every socket connect, I use a redis client to subscribe to a specific channel. Could this be used?
EDIT: I can get the return value from the client.publish callback to see how many subscribers received the message:
client.publish("somechannel", "some message", function(err, val){
console.log("here with val: " + val);
});
But this value does not always seem to be accurate.
EDIT: The reason I was getting inaccurate numbers from client.publish( is because I was incorrectly unsubscribing from the relevant channels. The above edit appears to work.
Here is the answer:
client.publish("somechannel", "some message", function(err, val){
console.log("here with val: " + val);
});