-
Notifications
You must be signed in to change notification settings - Fork 10.1k
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.