0
0
Node.jsframework~20 mins

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

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