What are Socket.IO rooms and namespaces?

Answer

Namespaces and Rooms are Socket.IO's two-level organization system for connections. A namespace is a separate communication channel within the same socket connection — essentially a path multiplexed over one TCP connection. Default namespace is /; you create custom ones with io.of('/chat'). Different namespaces can have different auth middleware. A room is a logical group within a namespace that sockets can join and leave dynamically. socket.join('room-123') adds the socket to a room; io.to('room-123').emit('event', data) broadcasts to all sockets in that room. Rooms are perfect for chat rooms, game sessions, or per-user notification channels. Unlike namespaces (separate resources), rooms are purely server-side groupings with no client-side counterpart.