0
0
Expressframework~20 mins

Broadcasting to rooms in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Room Broadcast Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when broadcasting to a room with no clients?

Consider this Express server using Socket.IO:

io.to('room42').emit('message', 'hello');

What is the behavior when no clients have joined room42?

ANo clients receive the message, but no error occurs.
BThe server throws an error because the room does not exist.
CThe message is queued until a client joins the room.
DThe message is sent to all clients connected to the server regardless of room membership.
Attempts:
2 left
💡 Hint

Think about how Socket.IO handles empty rooms when emitting.

📝 Syntax
intermediate
2:00remaining
Which code correctly broadcasts to all clients except the sender in a specific room?

Given a socket connection, which code sends a message to all clients in room1 except the sender?

Asocket.broadcast.to('room1').emit('event', 'data');
Bio.to('room1').emit('event', 'data');
Csocket.to('room1').emit('event', 'data');
Dio.broadcast.to('room1').emit('event', 'data');
Attempts:
2 left
💡 Hint

Remember that broadcast excludes the sender.

🔧 Debug
advanced
2:00remaining
Why does this broadcast not reach clients in the room?

Look at this code snippet:

socket.on('join', (room) => {
  socket.join(room);
});

socket.on('send', (room, msg) => {
  io.to(room).emit('message', msg);
});

Clients report they never receive the 'message' event. What is the most likely cause?

AThe server must use <code>socket.broadcast.to(room)</code> to send messages.
BClients never actually join the room because <code>socket.join(room)</code> is asynchronous and not awaited.
CThe <code>io.to(room).emit</code> call is incorrect; it should be <code>socket.to(room).emit</code>.
DThe client-side code is not listening for the 'message' event.
Attempts:
2 left
💡 Hint

Consider the asynchronous nature of socket.join.

state_output
advanced
2:00remaining
How many clients receive the message after these operations?

Assume 3 clients: A, B, and C.

Client A and B join roomX. Client C joins roomY.

Server runs:

io.to('roomX').emit('notify', 'hello');

How many clients receive the 'notify' event?

A1
B0
C3
D2
Attempts:
2 left
💡 Hint

Count clients in roomX.

🧠 Conceptual
expert
2:00remaining
What is the effect of using io.in(room).emit() vs io.to(room).emit()?

In Socket.IO, what is the difference between io.in(room).emit('event') and io.to(room).emit('event')?

A<code>io.in(room)</code> sends to all clients except the sender, <code>io.to(room)</code> includes the sender.
B<code>io.in(room)</code> sends only to the room creator, <code>io.to(room)</code> sends to all clients.
CThey are aliases and behave identically, sending to all clients in the room.
D<code>io.in(room)</code> sends to clients in nested rooms, <code>io.to(room)</code> sends only to the specified room.
Attempts:
2 left
💡 Hint

Check the official Socket.IO documentation for in and to.