What Are Socket.IO Rooms?

A room is a logical grouping of Socket.IO connections.

Rooms allow you to send events to a selected group of connected clients rather than broadcasting to every connected user.

Imagine a collaboration application with three projects:

  • Project A.
  • Project B.
  • Project C.

Users working on Project A should receive messages belonging to Project A only.

Instead of sending messages to every connected client, users join a room associated with their project.

A socket can join multiple rooms simultaneously.

Socket.IO automatically creates a private room for every socket using its socket ID. This allows you to target an individual connection using socket.id.

Rooms are managed on the server side and are not a substitute for application authorization. A user should only be allowed to join a room after the server verifies their permission.

Room-Based Communication
Key Takeaways
  • Rooms group connected sockets logically.

  • A socket can join multiple rooms.

  • Room-based broadcasting targets selected users.