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.
Previous
How do you implement a WebSocket server in Node.js using the `ws` library?
Next
How do you handle WebSocket reconnection logic?
More WebSockets & Real-time Questions
View all →- Intermediate How do you implement a WebSocket server in Node.js using the `ws` library?
- Intermediate How do you handle WebSocket reconnection logic?
- Intermediate What is the difference between Socket.IO and raw WebSockets?
- Intermediate How do you authenticate WebSocket connections?
- Intermediate How do WebSockets work behind a load balancer?