Namespaces and Rooms
In Socket.IO, namespaces and rooms are organizational features that help manage message broadcasting and isolate communication channels efficiently.[1][2][3]
Namespaces
A namespace defines a logical communication channel (like a subpath) under the same connection, separating parts of an application while reusing a single WebSocket.[2][4]
Key concepts:
- Created using
io.of('/namespace')on the server. - Clients connect using
io('/namespace'). - Used to separate application logic, such as admin dashboards, chat features, or analytics.[4][1]
Example:
// Server
const io = require('socket.io')(3000);
const chatNamespace = io.of('/chat');
chatNamespace.on('connection', socket => {
console.log('Client joined chat namespace');
socket.emit('welcome', 'Welcome to Chat!');
});
// Client
const socket = io('/chat');
socket.on('welcome', data => console.log(data));
Namespaces help in multiplexing—running different communication modules over one underlying connection while keeping them isolated.[5][4]
Rooms
A room is a smaller, server-side group within a namespace, allowing messages to target specific clients.[3][6]
Key points:
- Created dynamically with
socket.join('roomName'). - Clients can belong to multiple rooms.
- Used to send events to subsets of users, like chat rooms or private sessions.[7][3]
Example:
io.on('connection', socket => {
socket.on('joinRoom', roomName => {
socket.join(roomName);
io.to(roomName).emit('message', `A new user joined ${roomName}`);
});
socket.on('message', ({ room, text }) => {
io.to(room).emit('message', text);
});
});
Broadcasting options :[7]
io.to(room).emit(event, data)→ Send to all in a room.socket.to(room).emit(event, data)→ Send to all in a room except sender.io.to(['room1', 'room2']).emit(event, data)→ Target multiple rooms.
Comparison
| Feature | Namespace | Room | |
|---|---|---|---|
| Scope | Application-level separation | Subdivision within a namespace | |
| Client awareness | Client connects directly | Client unaware (managed by server) | |
| Creation | Defined in code | Dynamic, via join/leave | |
| Use case | Logical modules like “/chat” or “/admin” | Chat rooms, groups, notifications | |
| Connection overhead | Shared WebSocket | Same connection reused via groups | [1][2][3] |
Practical Advice
- Use namespaces when components must be isolated (e.g., user vs admin dashboards).
- Use rooms when segmenting users dynamically for broadcasting (e.g., per chatroom or project session).
Rooms provide lightweight message routing, while namespaces define distinct application layers.[1][4][7]