Bird
Raised Fist0
Node.jsframework~20 mins

Room and namespace concepts in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Socket.IO Room & Namespace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a client joins a room in a namespace?

Consider a Socket.IO server with namespaces and rooms. When a client connects to a namespace and joins a room, what is the effect on message delivery?

Node.js
const io = require('socket.io')(3000);
const chat = io.of('/chat');
chat.on('connection', socket => {
  socket.join('room1');
  chat.to('room1').emit('message', 'Hello Room 1');
});
AOnly clients connected to '/chat' namespace and joined 'room1' receive the 'message' event.
BAll clients connected to the server receive the 'message' event regardless of namespace or room.
COnly clients connected to the default namespace receive the 'message' event.
DClients in any namespace joined to 'room1' receive the 'message' event.
Attempts:
2 left
💡 Hint

Think about how namespaces isolate communication and rooms group clients within a namespace.

📝 Syntax
intermediate
2:00remaining
Which code correctly creates a namespace and joins a client to a room?

Identify the correct code snippet that creates a namespace '/news' and joins a client socket to a room 'updates'.

A
const news = io.of('/news');
news.on('connection', socket => {
  socket.join('updates');
});
B
const news = io.of('/news');
io.on('connection', socket => {
  socket.join('updates');
});
C
const news = io.of('/news');
news.on('connect', socket => {
  socket.join('updates');
});
D
const news = io.of('/news');
news.on('connection', socket => {
  news.join('updates');
});
Attempts:
2 left
💡 Hint

Remember the event name for new connections and how to join a room on the socket object.

🔧 Debug
advanced
2:30remaining
Why does emitting to a room not reach clients in another namespace?

Given this code, clients in '/chat' namespace joined to 'room1' do not receive messages emitted to 'room1' from '/news' namespace. Why?

const chat = io.of('/chat');
const news = io.of('/news');

chat.on('connection', socket => {
  socket.join('room1');
});

news.on('connection', socket => {
  news.to('room1').emit('message', 'Hello Room1');
});
AClients must join rooms on both namespaces to receive messages.
BThe 'to' method only works on the default namespace, not custom namespaces.
CRooms are scoped to namespaces, so 'room1' in '/news' is different from 'room1' in '/chat'.
DThe emit call is missing the socket object, so no clients receive the message.
Attempts:
2 left
💡 Hint

Think about how namespaces and rooms relate in Socket.IO.

❓ state_output
advanced
2:30remaining
How many clients receive the message after joining multiple rooms?

Consider a client socket that joins two rooms 'roomA' and 'roomB' in the same namespace. If the server emits a message to both rooms separately, how many times does the client receive the message?

socket.join('roomA');
socket.join('roomB');

namespace.to('roomA').emit('msg', 'Hello A');
namespace.to('roomB').emit('msg', 'Hello B');
AThe client receives one message with combined content from both rooms.
BThe client receives two separate messages: one from 'roomA' and one from 'roomB'.
CThe client receives only one message because rooms are merged.
DThe client receives no messages because it cannot join multiple rooms.
Attempts:
2 left
💡 Hint

Think about how emitting to rooms works and how clients receive events.

🧠 Conceptual
expert
3:00remaining
What is the main benefit of using namespaces with rooms in Socket.IO?

Why would a developer use namespaces combined with rooms instead of just rooms alone?

AUsing namespaces disables the need for rooms because they serve the same purpose.
BNamespaces automatically broadcast messages to all clients, rooms restrict messages to one client only.
CRooms are global across all namespaces, namespaces limit clients to one room only.
DNamespaces provide logical separation of communication channels, while rooms allow grouping within those channels for targeted messaging.
Attempts:
2 left
💡 Hint

Consider how namespaces and rooms organize clients differently.

Practice

(1/5)
1. What is the main purpose of a namespace in a Node.js real-time app using Socket.IO?
easy
A. To store user data persistently
B. To group users inside a single chat room
C. To divide the app into separate sections with their own event handling
D. To manage database connections

Solution

  1. Step 1: Understand namespaces in Socket.IO

    Namespaces create separate communication channels within the same server, allowing different parts of the app to handle events independently.
  2. Step 2: Differentiate namespaces from rooms

    Rooms group users inside namespaces, but namespaces themselves separate the app into sections with their own event handling.
  3. Final Answer:

    To divide the app into separate sections with their own event handling -> Option C
  4. Quick Check:

    Namespace = separate event handling sections [OK]
Hint: Namespaces separate event channels, rooms group users inside them [OK]
Common Mistakes:
  • Confusing namespaces with rooms
  • Thinking namespaces store data
  • Assuming namespaces manage databases
2. Which of the following is the correct way to create a namespace called /chat using Socket.IO in Node.js?
easy
A. const chat = io.namespace('/chat');
B. const chat = io.createNamespace('/chat');
C. const chat = io.room('/chat');
D. const chat = io.of('/chat');

Solution

  1. Step 1: Recall Socket.IO namespace creation syntax

    The correct method to create a namespace is io.of('/namespace').
  2. Step 2: Check each option

    const chat = io.of('/chat'); uses io.of('/chat'), which is correct. Others use invalid methods like namespace, room, or createNamespace.
  3. Final Answer:

    const chat = io.of('/chat'); -> Option D
  4. Quick Check:

    Namespace creation uses io.of() [OK]
Hint: Use io.of('/name') to create namespaces in Socket.IO [OK]
Common Mistakes:
  • Using io.namespace instead of io.of
  • Confusing rooms with namespaces
  • Trying to create namespaces with io.room
3. Given the code below, what will be the output when a client joins the room room1 and sends a message?
const io = require('socket.io')(3000);
const chat = io.of('/chat');
chat.on('connection', socket => {
  socket.join('room1');
  socket.on('message', msg => {
    chat.to('room1').emit('message', msg);
  });
});
medium
A. Only clients in 'room1' inside '/chat' receive the message
B. All clients in namespace '/chat' receive the message
C. Only the sender receives the message
D. No clients receive the message

Solution

  1. Step 1: Analyze the namespace and room usage

    The namespace is '/chat'. The socket joins 'room1' inside this namespace.
  2. Step 2: Understand the message emission

    The message is emitted to chat.to('room1'), meaning only clients in 'room1' within '/chat' get the message.
  3. Final Answer:

    Only clients in 'room1' inside '/chat' receive the message -> Option A
  4. Quick Check:

    chat.to('room1') targets room1 clients [OK]
Hint: chat.to('room') sends only to that room inside namespace [OK]
Common Mistakes:
  • Thinking message goes to all namespace clients
  • Assuming sender only gets message
  • Believing message is not sent at all
4. Identify the error in this code snippet that tries to send a message to a room inside a namespace:
const io = require('socket.io')(3000);
const news = io.of('/news');
news.on('connection', socket => {
  socket.join('updates');
  news.to('updates').emit('breaking', 'News update!');
});
medium
A. Missing await keyword before socket.join
B. Using news.to('updates') instead of socket.to('updates')
C. Joining a room before connection event
D. Not calling socket.emit inside the connection

Solution

  1. Step 1: Understand how to emit to a room excluding sender

    To send a message to all clients in a room except the sender, use socket.to('room').emit().
  2. Step 2: Identify misuse of news.to('updates')

    The code uses news.to('updates').emit(), which sends to all clients in the room including the sender, but this is not the correct pattern for broadcasting from a socket.
  3. Final Answer:

    Using news.to('updates') instead of socket.to('updates') -> Option B
  4. Quick Check:

    Broadcast to room uses socket.to(room) [OK]
Hint: Use socket.to(room) to broadcast inside namespace [OK]
Common Mistakes:
  • Using namespace object to emit instead of socket
  • Trying to join room outside connection event
  • Assuming socket.join is synchronous without await
5. You want to send a private message to a user in a specific room inside a namespace. Which approach correctly targets that user using Socket.IO?
hard
A. Use socket.to(userSocketId).emit('msg', message) inside the namespace
B. Use io.emit('msg', message) and filter on client side
C. Use namespace.to(room).emit('msg', message) and rely on room having only that user
D. Use namespace.emit('msg', message) without room or socket targeting

Solution

  1. Step 1: Understand private messaging in Socket.IO

    Private messages target a specific socket ID, not just a room, to ensure only one user receives it.
  2. Step 2: Analyze options for targeting a user

    Use socket.to(userSocketId).emit('msg', message) inside the namespace uses socket.to(userSocketId).emit(), which correctly sends a message to a specific socket inside the namespace.
  3. Step 3: Why other options fail

    Use namespace.to(room).emit('msg', message) and rely on room having only that user assumes room has only one user, which may not be true. Use io.emit('msg', message) and filter on client side sends to all clients and filters client-side, which is inefficient. Use namespace.emit('msg', message) without room or socket targeting sends to all namespace clients.
  4. Final Answer:

    Use socket.to(userSocketId).emit('msg', message) inside the namespace -> Option A
  5. Quick Check:

    Private message targets socket ID with socket.to(id) [OK]
Hint: Private message = socket.to(socketId).emit() inside namespace [OK]
Common Mistakes:
  • Assuming rooms always have one user
  • Broadcasting to all and filtering client-side
  • Not using socket ID for private messages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard