I have multiple socket.io servers scaled horizontally using a redisstore. I’ve got rooms setup effectively and am successfully able to broadcast to rooms across servers, etc. Now I’m trying to build a status page and what I’m failing on figuring out is how to simply count the number of users connected across all servers.
io.sockets.clients(‘room’) and io.sockets.sockets will only tell you the number of connected clients on that one server, not all servers connected to the same RedisStore.
Suggestions?
Thanks.
I solved this by having each server periodically set a user count in redis with an expiration that included their own pid:
every do
setex userCount:<pid> <interval+10> <count>then the status server can query for each of these keys, and then get the values for each key:
for each
keys userCount*do total+=get <key>so if a server crashes or is shutdown then its counts will drop out of redis after interval+10
sorry about the ugly pseudocode. 🙂