0
0
Node.jsframework~20 mins

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

Choose your learning style9 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.