Challenge - 5 Problems
WebSocket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Understanding WebSocket as an SSE Alternative
Which of the following best describes why WebSocket is often used as an alternative to Server-Sent Events (SSE) in Node.js applications?
Attempts:
2 left
💡 Hint
Think about the direction of data flow and interaction between client and server.
✗ Incorrect
WebSocket supports full-duplex communication, meaning both client and server can send messages independently. SSE only allows the server to push updates to the client.
❓ component_behavior
intermediate1:30remaining
Behavior of a WebSocket Server in Node.js
Given the following Node.js WebSocket server code snippet, what will happen when a client sends a message to the server?
Node.js
import { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); wss.on('connection', ws => { ws.on('message', message => { ws.send(`Server received: ${message}`); }); });
Attempts:
2 left
💡 Hint
Look at the ws.send call inside the message event handler.
✗ Incorrect
The server listens for messages from a client and sends a response back only to that client with the message prefixed.
📝 Syntax
advanced2:00remaining
Correct WebSocket Client Connection Syntax
Which of the following Node.js client code snippets correctly establishes a WebSocket connection to ws://localhost:8080 and logs messages received from the server?
Attempts:
2 left
💡 Hint
Consider the correct import and event listener syntax for Node.js WebSocket client.
✗ Incorrect
Option D correctly imports the WebSocket class from 'ws' and uses the 'on' method with 'message' event and a callback that logs the data.
🔧 Debug
advanced2:00remaining
Debugging WebSocket Server Connection Issue
A developer writes this WebSocket server code but clients cannot connect. What is the most likely cause?
Node.js
import { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 3000 }); wss.on('connection', ws => { ws.send('Welcome!'); });
Attempts:
2 left
💡 Hint
Check if the client connection URL matches the server port.
✗ Incorrect
The server listens on port 3000 but if clients try to connect to a different port like 8080, connections will fail.
❓ state_output
expert2:30remaining
Output of WebSocket Server Broadcast Implementation
Consider this Node.js WebSocket server code that broadcasts messages to all clients except the sender. What will be logged on the server console after two clients connect and one sends 'Hello'?
Node.js
import { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); wss.on('connection', ws => { console.log('Client connected'); ws.on('message', message => { console.log(`Received: ${message}`); wss.clients.forEach(client => { if (client !== ws && client.readyState === 1) { client.send(`Broadcast: ${message}`); } }); }); });
Attempts:
2 left
💡 Hint
Consider the order of events when clients connect and send messages.
✗ Incorrect
Each client connection triggers 'Client connected' log. When a message is received, 'Received: Hello' is logged. Broadcast messages are sent to clients but not logged on server.