Re: Counting socket.io users across horizontal servers

954 views
Skip to first unread message

Dan Shaw

unread,
Aug 28, 2012, 3:03:46 PM8/28/12
to sock...@googlegroups.com
There's nothing built into RedisStore to do this, but I've implemented
this several times. Here are the techniques I'd recommend:

If you need direct and frequent access to your connected user count, I
recommend using a separate redis client instance and maintaining a
count. Increment on connect and decrement on disconnect.

Then, you can expose the connection count per server on via a health
endpoint or to a repl and aggregate that data with your own tooling. I
share techniques for health and repl in my Production Node.js talk:
http://dshaw.github.com/2012-05-jsday/#/14/1
https://vimeo.com/46671305

Daniel Shaw
@dshaw


On Tue, Aug 28, 2012 at 11:43 AM, Ryan Curtis <wedn...@gmail.com> wrote:
> Hi!
>
> I'm using a RedisStore to scale my socketio servers horizontally, and I just
> want to be able to count the number of total connected users across all the
> socketio servers so that I can build a status page. Is there any way of
> doing this? Everything I've tried appears to only count connections to that
> specfic server.
>
> Thanks,
> Ryan

Dan Shaw

unread,
Aug 28, 2012, 4:10:59 PM8/28/12
to sock...@googlegroups.com
I wasn't proposing using pubsub at all for either solution. :) But, if
you're building a dashboard, then you could subscribe to the connect
and close events. Use redis-cli and run MONITOR to peek at the
internals.

I'd still persist connection count in Redis separately.
INCR statusserver:connections
DECR statusserver:connections

Daniel Shaw
@dshaw


On Tue, Aug 28, 2012 at 12:30 PM, Ryan Curtis <wedn...@gmail.com> wrote:
> or are you explicitly publishing to pubsub on user connect?
>
>
> On Tuesday, August 28, 2012 3:26:44 PM UTC-4, Ryan Curtis wrote:
>>
>> Ok, that sounds viable, but the devil's in the details... I'm working on a
>> statusserver that is another socket.io server using the same redis store.
>> How can it get notifications that someone connected on another server? Does
>> a connection message show up via pubsub when a user connects?

Ryan Curtis

unread,
Oct 5, 2012, 10:09:31 AM10/5/12
to sock...@googlegroups.com
I used Object.keys(io.sockets.sockets).length, but have found that doesn't count correctly either, I'm guessing for the same reasons you outline, though that wasn't obvious in testing.  I've just found the number grows and shrinks as people connect and disconnect, but grows more than it shrinks so that after a day or two it seems like way more people are connected than actually are.  Seems like a bug to me, and I worry that it means that memory usage will slowly grow without bounds but anyway..

We have a presence system as part of our client, meaning that when a user connects we store their user object with socket.set, and then mark that user as 'active'.  When their client goes idle (no mouse movement etc) after some time, the client tells the socket server they are idle.  when they disconnect then they get marked as 'offline'.  What I'm doing now to get an accurate count is looping through the sockets, pulling their user object using socket.get, checking their presence state and if it is 'active' then I count them.  This way I have an accurate count of active users, and it seems to work well.

On Wednesday, October 3, 2012 5:07:25 PM UTC-4, Charlie wrote:
Ryan, how are you getting the count for each server's users?  The result of io.sockets.clients().length isn't always correct.

For example:

1. Process A is running and 2 clients connect.  io.sockets.clients().length will correctly return 2.
2. Start a new process, B, and connect 2 clients to it.  B will return 2, however A will now return 4 because it has subscribed to B's connection events.

Counts get even more inaccurate when you try restarting a server and clients reconnect.

What's the best way of getting either a single server's _real_ connected clients count?  Or even better yet a way to easily get the total count at any one point, taking into account server restarts etc.?


On Tuesday, August 28, 2012 11:53:59 PM UTC+1, Ryan Curtis wrote:
Ok, I think I have a working solution.
Each realtime server knows how many users it has, so each can set a count with an expiration on an interval a bit shorter than the expiration.  IE every interval/2 'setex usercount:<pid> <interval> <count>'
Then the statusserver can query 'keys usercount*', get each of those keys and add the totals.  If a realtime server dies then its numbers will expire and won't add to the count any more once <interval> expires. 

Thanks for the help.

Ryan

Filipe

unread,
Sep 27, 2013, 2:06:48 PM9/27/13
to sock...@googlegroups.com
Guys,

Were you able to do this without the use of workarounds?

Best regards,

Filipe Deschamps

Ryan Curtis

unread,
Sep 27, 2013, 2:35:24 PM9/27/13
to sock...@googlegroups.com
No. I think the best answer is "maintain your own count".


--
You received this message because you are subscribed to a topic in the Google Groups "Socket.IO" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/socket_io/O0lJzKaZSfA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to socket_io+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages