Bird
Raised Fist0
Expressframework~20 mins

Broadcasting to rooms in Express - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of broadcasting to rooms in Express with Socket.IO?
easy
A. To send messages to specific groups of connected clients
B. To store user data permanently on the server
C. To create new HTTP routes automatically
D. To increase server CPU usage

Solution

  1. Step 1: Understand broadcasting concept

    Broadcasting to rooms means sending messages only to clients in a specific group or room.
  2. Step 2: Identify purpose in Socket.IO

    Socket.IO uses rooms to organize clients so messages can be sent efficiently to those groups.
  3. Final Answer:

    To send messages to specific groups of connected clients -> Option A
  4. Quick Check:

    Broadcasting to rooms = targeted messaging [OK]
Hint: Rooms group clients for targeted messages [OK]
Common Mistakes:
  • Thinking rooms store data permanently
  • Confusing rooms with HTTP routes
  • Assuming rooms increase CPU usage
2. Which of the following is the correct syntax to send a message to all clients in a room named chatroom using Socket.IO's io object?
easy
A. io.to('chatroom').emit('message', 'Hello everyone!');
B. io.emit.to('chatroom')('message', 'Hello everyone!');
C. io.in('chatroom').send('message', 'Hello everyone!');
D. io.room('chatroom').emit('message', 'Hello everyone!');

Solution

  1. Step 1: Recall correct method chaining

    Socket.IO uses io.to(room).emit(event, data) to send messages to a room.
  2. Step 2: Check each option's syntax

    io.to('chatroom').emit('message', 'Hello everyone!'); matches the correct syntax exactly; others have incorrect method order or names.
  3. Final Answer:

    io.to('chatroom').emit('message', 'Hello everyone!'); -> Option A
  4. Quick Check:

    Use io.to(room).emit() for room broadcast [OK]
Hint: Use io.to(room).emit() to broadcast [OK]
Common Mistakes:
  • Using io.emit.to() instead of io.to().emit()
  • Using io.in().send() which is invalid
  • Using io.room() which does not exist
3. Consider this code snippet using Socket.IO:
socket.join('room1');
io.to('room1').emit('greet', 'Hi Room 1!');
What will happen when this code runs?
medium
A. No clients receive the event because of syntax error
B. All connected clients receive the 'greet' event
C. Only the client who joined 'room1' receives the 'greet' event
D. Only clients in 'room1' receive the 'greet' event

Solution

  1. Step 1: Understand socket.join()

    The socket.join('room1') adds the current client to the room named 'room1'.
  2. Step 2: Analyze io.to('room1').emit()

    This sends the 'greet' event to all clients currently in 'room1'.
  3. Final Answer:

    Only clients in 'room1' receive the 'greet' event -> Option D
  4. Quick Check:

    io.to(room).emit() targets room clients only [OK]
Hint: io.to(room).emit() sends to all in that room [OK]
Common Mistakes:
  • Thinking all clients get the event
  • Assuming only the joining client gets the event
  • Believing syntax error prevents sending
4. Identify the error in this Socket.IO code snippet:
socket.join('roomA');
io.to('roomA').emit('update', 'New data');
socket.leave('roomA');
io.to('roomA').emit('update', 'More data');
medium
A. The second emit will not reach any clients because the socket left the room
B. The code will throw a runtime error on socket.leave()
C. Clients in 'roomA' will receive both 'update' events
D. The first emit will not send because socket.join() is asynchronous

Solution

  1. Step 1: Understand socket.join() and leave()

    Joining and leaving rooms affects only the current socket, not others in the room.
  2. Step 2: Analyze emits after leave()

    Even after the socket leaves, other clients in 'roomA' still receive the second emit.
  3. Final Answer:

    Clients in 'roomA' will receive both 'update' events -> Option C
  4. Quick Check:

    Leaving room affects only that socket, others still get messages [OK]
Hint: Leaving room affects only that socket, not all clients [OK]
Common Mistakes:
  • Assuming no clients get second emit
  • Expecting runtime error on leave()
  • Thinking join() is asynchronous and blocks emit
5. You want to send a message to all clients in multiple rooms: room1 and room2. Which code correctly broadcasts to both rooms without sending duplicate messages to clients in both rooms?
hard
A. io.to('room1').emit('notice', 'Hello!'); io.to('room2').emit('notice', 'Hello!');
B. io.to('room1').to('room2').emit('notice', 'Hello!');
C. io.in('room1, room2').emit('notice', 'Hello!');
D. io.to(['room1', 'room2']).emit('notice', 'Hello!');

Solution

  1. Step 1: Understand chaining to() calls

    Chaining io.to('room1').to('room2') sends to clients in either room without duplicates.
  2. Step 2: Analyze other options

    io.to(['room1', 'room2']).emit('notice', 'Hello!'); is invalid syntax; io.in('room1, room2').emit('notice', 'Hello!'); sends to a single room named 'room1, room2'; io.to('room1').emit('notice', 'Hello!'); io.to('room2').emit('notice', 'Hello!'); sends separately causing duplicates for clients in both rooms.
  3. Final Answer:

    io.to('room1').to('room2').emit('notice', 'Hello!'); -> Option B
  4. Quick Check:

    Chain io.to(room) to broadcast to multiple rooms without duplicates [OK]
Hint: Chain io.to(room) calls to avoid duplicate messages [OK]
Common Mistakes:
  • Using array inside io.to() which is invalid
  • Sending separate emits causing duplicates
  • Using comma-separated room names like 'room1, room2'
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