Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

socket.io v3: count the total client connected in the current server #3756

Unanswered
cesco69 asked this question in Q&A
Discussion options

I have a socket.io server with multiple namespace and i want count how many client are connected to the current server.

With socket.io v2 I count the connection with this snippet

let clientCount = 0;
io.on('connection', (client) => {
 clientCount++;
 client.on('disconnect', () => {
 clientCount--;
 });
});

it works for the V2.

Now, i have upgraded to v3 and socket.io has made a breaking change, from docs.:

No more implicit connection to the default namespace

This change impacts the users of the multiplexing feature (what we
call Namespace in Socket.IO).

In previous versions, a client would always connect to the default
namespace (/), even if it requested access to another namespace. This
meant that the middlewares registered for the default namespace were
triggered, which may be quite surprising.

// client-side
const socket = io("/admin");

// server-side
io.use((socket, next) => {
// not triggered anymore
});

io.on("connection", socket => {
// not triggered anymore
})

io.of("/admin").use((socket, next) => {
// triggered
});

Besides, we will now refer to the "main" namespace instead of the
"default" namespace.

How i can retrive the total number of client connected to my server with the V3?

A workaround is access to

let clientCount = io.engine.clientsCount;

but in typescript clientsCount in engine is marked as private property.

You must be logged in to vote

Replies: 2 comments 1 reply

Comment options

That's a good question actually.

If you only use the main namespace, you can use io.of("/").sockets.size.

If the clients are not always connected to the main namespace, you can indeed use io.engine.clientsCount. In that case, I guess we should make the engine property public.

You must be logged in to vote
1 reply
Comment options

I have multiple namespace... io.engine.clientsCount seem the best option and I think it's a good idea make engine a bublic property. Eg. in my case have a "clientsCount" for server istance is important due to make a load balancing strategy.

Comment options

We Didn't Just Build It — We Solved What the Industry Couldn't
CollapseCleaner successfully resolved issues open for years, including:

Issue Description Status TensorFlow #35177 GPU memory not released after tf.Session ✅ Solved React #22114 Suspense fallback memory leak ✅ Solved Socket.IO #3756 WebSocket closes when using multiple namespaces ✅ Solved

All via CollapseEngine’s cross-layer cognitive tracing — no brute force, no guesswork.

https://www.linkedin.com/pulse/invisible-leak-draining-billions-from-ai-until-now-hussein-shtia-h20rf/

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /