socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Count of connected clients/sockets

Open denisu opened this issue 15 years ago • 53 comments

Hi,

I am using io.sockets.clients().length to get the count of all currently connected clients, but after some time this number is way to high, many disconnected clients are still present in this array.

Below an image of io.sockets.clients().length, plotted every 5 minutes for 24 hours. Once the count reached over ~2k, i restarted the server, about 200 to 300 clients reconnected, which is the correct count.

Connected Clients

Is this the wrong way to get the count of all connected clients? I also tried to count the connects and disconnects manually, but after one day there were more disconnects than connects, the count was negative.

Related: https://github.com/LearnBoost/socket.io/issues/349

denisu avatar Aug 14 '11 16:08 denisu

+1. I think in my case this is one last issue causing memory problems (growing until segmentation fault).

nookiepl avatar Aug 18 '11 08:08 nookiepl

Yep, crashes about twice a day with a segmentation fault here.

denisu avatar Aug 21 '11 09:08 denisu

its noted in the "documentation": http://labs.learnboost.com/socket.io/

under Socket.IO Node.JS server / Listener section (do an in-page search for "splice"). To quote:

"An array of clients. Important: disconnected clients are set to null, the array is not spliced."

To make it from thought to code to documentation means there has to be some reason why disconnected user's aren't pruned from this array, I just can't guess what that reason is. Best case scenario is that it makes keeping track of CONNECTED users a pain, and at worst is contributing to a lack of stability in a node.js/socket.io server implementation.

rickygrows avatar Sep 07 '11 20:09 rickygrows

Marked as performance bug.

3rd-Eden avatar Sep 19 '11 22:09 3rd-Eden

i have the same problem.

and i make my number base on some var with i +1 during connect event and -1 during disconnect. (so this is not null problem)

Usercount:

https://skitch.com/tomekotomek/f4867/dock

CPU:

https://skitch.com/tomekotomek/f489w/dock

MEMORY:

https://skitch.com/tomekotomek/f4eyy/graph.php-747x374

BTW.

it is more then performance problem it just lower our up-time to less then one week.

dymsza avatar Sep 19 '11 23:09 dymsza

The way I figured how to do this was to use a Redis set of the client ids, add on the socket.io connect and remove from the set on disconnect. SCARD command gives you the # of clients connected and SMEMBERS all the ids.

ErneX avatar Nov 03 '11 18:11 ErneX

yeah the array needs to cleared or a better way needs to found to hold clients.

dmportella avatar Dec 07 '11 14:12 dmportella

+1

xpepermint avatar Jan 18 '12 22:01 xpepermint

+1

austinh avatar Feb 06 '12 01:02 austinh

+1 This is definitely more than just an array length issue. Incrementing and decrementing a counter on connect/disconnect will still result in an ever increasing counter.

desheikh avatar Feb 06 '12 05:02 desheikh

+1

hinamkim avatar Mar 16 '12 06:03 hinamkim

+1

nikek avatar Apr 08 '12 23:04 nikek

+1

digitalmio avatar Apr 09 '12 19:04 digitalmio

+1

englercj avatar Apr 11 '12 12:04 englercj

+1

Luoti avatar Apr 13 '12 05:04 Luoti

Has anyone tried to isolate this to the various transports? I.E. See if it works the same in websocket, flashsocket, xhr-polling, etc.

This is definitely an important issue as we can't leave the server running consistently without a restart. I can't even go a day without at least one restart as memory continues to grow. NOTE: In node 0.4.* memory is MUCH BETTER for me.

crickeys avatar Apr 16 '12 16:04 crickeys

+1

anthonywebb avatar Apr 27 '12 17:04 anthonywebb

i discovered the same problem - my memory just gets filled up. about 1 mb / per minute

with about 1500 sockets which reconnect due to clicks on the site havy.

it has to be restarted daily because of the memory leak

tommypowerz avatar May 01 '12 19:05 tommypowerz

L2C FAF IT RTFM!!! ░░░░░▄▄▄▄▀▀▀▀▀▀▀▀▄▄▄▄▄▄░░░░░░░░ ░░░░░█░░░░▒▒▒▒▒▒▒▒▒▒▒▒░░▀▀▄░░░░ ░░░░█░░░▒▒▒▒▒▒░░░░░░░░▒▒▒░░█░░░ ░░░█░░░░░░▄██▀▄▄░░░░░▄▄▄░░░░█░░ ░▄▀▒▄▄▄▒░█▀▀▀▀▄▄█░░░██▄▄█░░░░█░ █░▒█▒▄░▀▄▄▄▀░░░░░░░░█░░░▒▒▒▒▒░█ █░▒█░█▀▄▄░░░░░█▀░░░░▀▄░░▄▀▀▀▄▒█ ░█░▀▄░█▄░█▀▄▄░▀░▀▀░▄▄▀░░░░█░░█░ ░░█░░░▀▄▀█▄▄░█▀▀▀▄▄▄▄▀▀█▀██░█░░ ░░░█░░░░██░░▀█▄▄▄█▄▄█▄████░█░░░ ░░░░█░░░░▀▀▄░█░░░█░█▀██████░█░░ ░░░░░▀▄░░░░░▀▀▄▄▄█▄█▄█▄█▄▀░░█░░ ░░░░░░░▀▄▄░▒▒▒▒░░░░░░░░░░▒░░░█░ ░░░░░░░░░░▀▀▄▄░▒▒▒▒▒▒▒▒▒▒░░░░█░ ░░░░░░░░░░░░░░▀▄▄▄▄▄░░░░░░░░█░░ ░░░░░░░░░░░░░░░░░░░░▀▀▀▀▀▀▀▀░░░

dmportella avatar May 02 '12 09:05 dmportella

I just turned off the flashsocket transport server side and memory usage AND cpu usage is SO MUCH BETTER. Can anyone else try that out and see if that helps?

crickeys avatar May 03 '12 21:05 crickeys