Performance: Rooms and namespaces
This concept affects real-time communication performance by controlling message routing and reducing unnecessary network traffic.
io.to('room1').emit('message', data);
io.emit('message', data);| Pattern | Network Traffic | Client Processing | Event Handling | Verdict |
|---|---|---|---|---|
| Broadcast to all clients | High | High | High | [X] Bad |
| Emit to specific room | Low | Low | Medium | [OK] Good |
| Single namespace for all events | Medium | Medium | High | [!] OK |
| Separate namespaces per module | Low | Low | Low | [OK] Good |
this.server.to('room1').emit('message', 'Hello Room1');to('room1') sends the event only to clients in that room.@SubscribeMessage('joinRoom')
handleJoin(client: any, payload: { room: string }) {
client.join(payload.room);
client.emit('joined', `Joined room ${payload.room}`);
}client parameter should be typed as Socket (from socket.io). Typing as any lacks type safety, intellisense, and TypeScript checks for methods like join().client.emit works.Pass the test and this pattern moves unknown