Count of connected clients/sockets
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.

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
+1. I think in my case this is one last issue causing memory problems (growing until segmentation fault).
Yep, crashes about twice a day with a segmentation fault here.
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.
Marked as performance bug.
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.
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.
yeah the array needs to cleared or a better way needs to found to hold clients.
+1
+1
+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.
+1
+1
+1
+1
+1
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.
+1
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
L2C FAF IT RTFM!!! ░░░░░▄▄▄▄▀▀▀▀▀▀▀▀▄▄▄▄▄▄░░░░░░░░ ░░░░░█░░░░▒▒▒▒▒▒▒▒▒▒▒▒░░▀▀▄░░░░ ░░░░█░░░▒▒▒▒▒▒░░░░░░░░▒▒▒░░█░░░ ░░░█░░░░░░▄██▀▄▄░░░░░▄▄▄░░░░█░░ ░▄▀▒▄▄▄▒░█▀▀▀▀▄▄█░░░██▄▄█░░░░█░ █░▒█▒▄░▀▄▄▄▀░░░░░░░░█░░░▒▒▒▒▒░█ █░▒█░█▀▄▄░░░░░█▀░░░░▀▄░░▄▀▀▀▄▒█ ░█░▀▄░█▄░█▀▄▄░▀░▀▀░▄▄▀░░░░█░░█░ ░░█░░░▀▄▀█▄▄░█▀▀▀▄▄▄▄▀▀█▀██░█░░ ░░░█░░░░██░░▀█▄▄▄█▄▄█▄████░█░░░ ░░░░█░░░░▀▀▄░█░░░█░█▀██████░█░░ ░░░░░▀▄░░░░░▀▀▄▄▄█▄█▄█▄█▄▀░░█░░ ░░░░░░░▀▄▄░▒▒▒▒░░░░░░░░░░▒░░░█░ ░░░░░░░░░░▀▀▄▄░▒▒▒▒▒▒▒▒▒▒░░░░█░ ░░░░░░░░░░░░░░▀▄▄▄▄▄░░░░░░░░█░░ ░░░░░░░░░░░░░░░░░░░░▀▀▀▀▀▀▀▀░░░
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?