Bird
Raised Fist0
Node.jsframework~20 mins

Broadcasting to connected clients in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Broadcasting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when broadcasting a message to all clients?
Consider a Node.js server using WebSocket. When the server broadcasts a message to all connected clients, what will each client receive?
Node.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    wss.clients.forEach(function each(client) {
      if (client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });
});
ANo clients receive any message because broadcast is not implemented.
BOnly the client that sent the message receives it back.
COnly clients connected after the message is sent receive it.
DAll connected clients receive the exact same message sent by one client.
Attempts:
2 left
💡 Hint
Think about how the server loops through all clients and sends the message.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in broadcasting code
Which option contains a syntax error that prevents broadcasting messages to all clients?
Node.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', ws => {
  ws.on('message', message => {
    wss.clients.forEach(client => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(message)
      }
    });
  });
});
AMissing closing parenthesis for wss.on('connection'...)
BUsing parentheses instead of curly braces in forEach callback
CMissing semicolon after client.send(message)
DUsing assignment operator '=' instead of comparison '===' in if condition
Attempts:
2 left
💡 Hint
Check the if condition inside the forEach loop carefully.
🔧 Debug
advanced
2:00remaining
Why does broadcasting fail when clients disconnect?
Given this broadcasting code, why might some clients not receive messages after disconnecting and reconnecting?
Node.js
wss.on('connection', ws => {
  ws.on('message', message => {
    wss.clients.forEach(client => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });
});
AThe server does not remove closed clients from wss.clients, so readyState check fails.
BClients reconnecting get a new WebSocket instance not yet in wss.clients.
Cwss.clients only contains clients connected at server start, not new ones.
DDisconnected clients remain in wss.clients, causing errors when sending.
Attempts:
2 left
💡 Hint
Think about how the server tracks connected clients and their states.
❓ state_output
advanced
2:00remaining
What is the state of clients after broadcasting a message?
After broadcasting a message to all clients, what is the state of each client's WebSocket connection?
Node.js
wss.clients.forEach(client => {
  if (client.readyState === WebSocket.OPEN) {
    client.send('Hello');
  }
});
AAll clients remain OPEN and receive the message.
BClients change state to CLOSING after receiving the message.
CClients become CLOSED immediately after message is sent.
DClients' state is UNKNOWN after sending the message.
Attempts:
2 left
💡 Hint
Sending a message does not close the connection.
🧠 Conceptual
expert
3:00remaining
Which option best describes broadcasting scalability challenges?
When broadcasting messages to many connected clients in Node.js WebSocket server, what is a key scalability challenge?
ABroadcasting automatically queues messages on clients, so no server load increase occurs.
BBroadcasting to many clients can cause high CPU and memory usage due to many simultaneous sends.
CWebSocket protocol limits the number of clients to 100, so scalability is not an issue.
DBroadcasting messages is handled by the browser, so server resources are unaffected.
Attempts:
2 left
💡 Hint
Consider what happens on the server when sending many messages at once.

Practice

(1/5)
1. What does broadcasting mean in the context of Node.js server with socket.io?
easy
A. Sending the same message to all connected clients at once
B. Sending a message to only one client
C. Receiving messages from clients
D. Disconnecting all clients simultaneously

Solution

  1. Step 1: Understand broadcasting concept

    Broadcasting means sending one message to every connected client at the same time.
  2. Step 2: Compare options

    Only Sending the same message to all connected clients at once describes sending the same message to all clients simultaneously, which matches broadcasting.
  3. Final Answer:

    Sending the same message to all connected clients at once -> Option A
  4. Quick Check:

    Broadcasting = send to all clients [OK]
Hint: Broadcasting means one message to all clients at once [OK]
Common Mistakes:
  • Confusing broadcasting with sending to one client
  • Thinking broadcasting disconnects clients
  • Mixing up receiving and sending messages
2. Which of the following is the correct syntax to broadcast a message to all clients using socket.io in Node.js?
easy
A. io.send('message', data);
B. io.broadcast('message', data);
C. socket.emit('message', data);
D. io.emit('message', data);

Solution

  1. Step 1: Identify broadcasting method in socket.io

    The method to send a message to all connected clients is io.emit.
  2. Step 2: Check each option's correctness

    io.emit('message', data); uses io.emit, which is correct. io.broadcast('message', data); uses a non-existent broadcast method on io. socket.emit('message', data); sends only to one socket. io.send('message', data); uses send which is not the broadcasting method.
  3. Final Answer:

    io.emit('message', data); -> Option D
  4. Quick Check:

    Broadcast method = io.emit [OK]
Hint: Use io.emit to broadcast messages to all clients [OK]
Common Mistakes:
  • Using socket.emit instead of io.emit
  • Trying io.broadcast which does not exist
  • Confusing send with emit
3. Given the following code snippet, what will be the output on all connected clients?
io.on('connection', (socket) => {
  io.emit('news', { hello: 'world' });
});
medium
A. Only the newly connected client receives { hello: 'world' }
B. All connected clients receive { hello: 'world' }
C. No clients receive any message
D. Only the server logs { hello: 'world' }

Solution

  1. Step 1: Understand io.emit inside connection event

    Inside the connection event, io.emit sends the message to all clients, not just the new one.
  2. Step 2: Determine who receives the message

    Since io.emit broadcasts, every connected client, including the new one, receives the message { hello: 'world' }.
  3. Final Answer:

    All connected clients receive { hello: 'world' } -> Option B
  4. Quick Check:

    io.emit sends to all clients [OK]
Hint: io.emit inside connection sends to all clients [OK]
Common Mistakes:
  • Thinking only new client gets the message
  • Assuming no message is sent
  • Confusing io.emit with socket.emit
4. Identify the error in this code snippet intended to broadcast a message to all clients:
io.on('connection', (socket) => {
  socket.broadcast.emit('update', { status: 'new' });
});
medium
A. It will send the message to all clients except the sender
B. It will send the message only to the sender
C. It will send the message to all clients including the sender
D. It causes a syntax error

Solution

  1. Step 1: Understand socket.broadcast.emit behavior

    The socket.broadcast.emit sends the message to all clients except the one that triggered the event.
  2. Step 2: Analyze the code's effect

    Since the message is sent with socket.broadcast.emit, the sender does NOT receive the message, only others do.
  3. Final Answer:

    It will send the message to all clients except the sender -> Option A
  4. Quick Check:

    socket.broadcast.emit excludes sender [OK]
Hint: socket.broadcast.emit excludes sender client [OK]
Common Mistakes:
  • Thinking broadcast sends to all including sender
  • Confusing broadcast with io.emit
  • Assuming syntax error
5. You want to broadcast a notification to all clients except the one who triggered an event. Which code snippet correctly achieves this?
hard
A. io.emit('notify', message);
B. socket.emit('notify', message);
C. socket.broadcast.emit('notify', message);
D. io.broadcast.emit('notify', message);

Solution

  1. Step 1: Identify method to exclude sender

    To send to all except the sender, use socket.broadcast.emit.
  2. Step 2: Check each option

    io.emit('notify', message); sends to all clients including sender. socket.emit('notify', message); sends only to sender. socket.broadcast.emit('notify', message); correctly sends to all except sender. io.broadcast.emit('notify', message); uses a non-existent method.
  3. Final Answer:

    socket.broadcast.emit('notify', message); -> Option C
  4. Quick Check:

    Exclude sender = socket.broadcast.emit [OK]
Hint: Use socket.broadcast.emit to exclude sender from broadcast [OK]
Common Mistakes:
  • Using io.emit when sender exclusion needed
  • Trying io.broadcast.emit which does not exist
  • Using socket.emit which sends only to sender
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