0
0
Node.jsframework~10 mins

Room and namespace concepts in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Room and namespace concepts
Client connects
Join Namespace
Join Room
Send message
Message delivered to clients in same room
Client disconnects
Clients connect, join a namespace, then join a room inside it. Messages sent to a room reach only clients in that room.
Execution Sample
Node.js
const io = require('socket.io')(3000);
io.of('/chat').on('connection', socket => {
  socket.join('room1');
  socket.to('room1').emit('message', 'Hello Room1');
});
A server listens on namespace '/chat', clients join 'room1', and messages are sent only to that room.
Execution Table
StepActionNamespaceRoomMessage SentClients Receiving
1Client connectsnonenonenonenone
2Client joins namespace '/chat'/chatnonenonenone
3Client joins room 'room1'/chatroom1nonenone
4Server emits message to 'room1'/chatroom1'Hello Room1'Clients in room1
5Client disconnectsnonenonenonenone
💡 Client disconnects, ending the session and leaving rooms and namespaces.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
Namespacenone/chat/chat/chatnone
Roomnonenoneroom1room1none
Message Sentnonenonenone'Hello Room1'none
Clients ReceivingnonenonenoneClients in room1none
Key Moments - 3 Insights
Why does a message sent to a room only reach clients in that room?
Because the server emits the message scoped to that room inside the namespace, as shown in step 4 of the execution_table.
Can a client be in multiple rooms at once?
Yes, clients can join multiple rooms within a namespace, but this example shows only one room for simplicity (step 3).
What happens when a client disconnects?
The client leaves all rooms and namespaces automatically, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the client join a room?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Room' column in the execution_table to see when it changes from 'none' to 'room1'.
According to variable_tracker, what is the Namespace value after step 4?
A/room1
Bnone
C/chat
Dundefined
💡 Hint
Look at the 'Namespace' row and the 'After Step 4' column in variable_tracker.
If the server emits a message outside any room, who receives it?
AOnly clients in the default room
BAll clients in the namespace
CNo clients receive it
DOnly clients in 'room1'
💡 Hint
Think about how namespaces and rooms control message delivery; messages outside rooms go to all namespace clients.
Concept Snapshot
Rooms and namespaces organize clients in Socket.IO.
Namespaces separate groups of clients logically.
Rooms are sub-groups inside namespaces.
Messages sent to a room reach only clients in that room.
Clients can join multiple rooms.
Disconnecting removes clients from all rooms and namespaces.
Full Transcript
In Socket.IO, clients first connect to the server, then join a namespace, which is like a channel. Inside a namespace, clients can join rooms, smaller groups for targeted messaging. When the server sends a message to a room, only clients in that room get it. Clients can be in multiple rooms at once. When a client disconnects, it leaves all rooms and namespaces automatically. This helps organize communication efficiently.