How to get a list of connected sockets in Socket.IO for all versions

After hours of debugging, I figured out the right way to get a list of connected sockets in Socket.io. This post is a list of all of them, for all major versions of socket.io.

I love Socket.io. It makes life easier for tons of WebSocket developers with simple setup and easy-to-use methods.

But one thing that’s not very clear is that with every new Socket.io version, they tend to keep changing a lot of the syntax which breaks code running in older versions.

One of the common use cases of socket.io is to get a list of everyone who is connected to your server. This simple function is notoriously known to be done in many ways across many different versions, and today, I will explain how to use Socket.io to get a list of connected clients on all major versions the intended way.

Hope it helps!

Version 4.x (Latest)

The easiest way to get all the connected sockets is through:

await io.fetchSockets()

It returns an array of all connected sockets

Link to Documentation of this method

// Return all Socket instances
const sockets = await io.fetchSockets();

// Return all Socket instances in the "room1" room of the main namespace
const sockets = await io.in("room1").fetchSockets();

// Return all Socket instances in the "room1" room of the "admin" namespace
const sockets = await io.of("/admin").in("room1").fetchSockets();

// This also works with a single socket ID
const sockets = await io.in(theSocketId).fetchSockets();

Example usages

// With an async function

 const sockets = await io.fetchSockets()
 sockets.forEach(socket => {
    // Do something
 });
// Without an async function

io.fetchSockets()
.then((sockets) => {
  sockets.forEach((socket) => {
    // Do something
  })
})
.catch(console.log)

Version 3.x

Use the following method:

namespace.allSockets()

  • Returns Promise<Set<SocketId>>

Gets a list of socket IDs connected to this namespace (across all nodes if applicable).

// all sockets in the main namespace
const ids = await io.allSockets();

// all sockets in the main namespace and in the "user:1234" room
const ids = await io.in("user:1234").allSockets();

// all sockets in the "chat" namespace
const ids = await io.of("/chat").allSockets();

// all sockets in the "chat" namespace and in the "general" room
const ids = await io.of("/chat").in("general").allSockets();

Version 2.x

Use the following method:

for (let [id, socket] of io.of("/").sockets) {
    // Do something
}

We are looping over the io.of(“/”).sockets object, which is a Map of all currently connected Socket instances, indexed by ID.

Link to Documentation of this method.

Version 1.x

To count all clients connected to 'my_room' :

1.4+:

var room = io.sockets.adapter.rooms['my_room'];
room.length; // An array

1.3.x:

var room = io.sockets.adapter.rooms['my_room'];
Object.keys(room).length; // room is an object

1.0.x to 1.2.x:

var room = io.adapter.rooms['my_room'];
Object.keys(room).length; // also an object

Version 0.x

In Socket.IO 0.7 you have a clients method on the namespaces. This returns an array of all connected sockets.

API for no namespace:

var clients = io.sockets.clients();
var clients = io.sockets.clients('room'); // all users from room `room`

For a namespace

var clients = io.of('/chat').clients();
var clients = io.of('/chat').clients('room'); // all users from room `room`

Conclusion

These are all the ways to get the list of connected sockets in all Socket.io versions. It took me a while to compile, so if you find any mistakes please comment below.

Further Reading:

Thanks for reading and hope this helped!

5 Likes