Challenge - 5 Problems
WebSocket Server Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a client sends a message?
Consider this WebSocket server code using the ws library. What will the server send back to the client when the client sends the message "hello"?
Node.js
import WebSocket, { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); wss.on('connection', function connection(ws) { ws.on('message', function message(data) { ws.send(`Received: ${data}`); }); });
Attempts:
2 left
💡 Hint
Look at what the server sends back inside the message event handler.
✗ Incorrect
The server listens for messages and sends back a string prefixed with "Received: ". So when the client sends "hello", the server replies with "Received: hello".
❓ lifecycle
intermediate1:30remaining
Which event triggers when a client disconnects?
In the ws WebSocket server, which event should you listen to detect when a client closes the connection?
Node.js
import WebSocket, { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); wss.on('connection', function connection(ws) { // Which event here? ws.on('???', () => { console.log('Client disconnected'); }); });
Attempts:
2 left
💡 Hint
Think about the standard WebSocket event for connection closure.
✗ Incorrect
The 'close' event fires when the client disconnects. Other events like 'disconnect' or 'end' are not used in ws.
📝 Syntax
advanced1:30remaining
Which option correctly creates a WebSocket server on port 3000?
Choose the code snippet that correctly creates a WebSocket server listening on port 3000 using the ws library.
Attempts:
2 left
💡 Hint
Check the correct way to pass options to the WebSocketServer constructor.
✗ Incorrect
The ws library uses new WebSocketServer({ port: 3000 }) to create a server. Other options are invalid syntax or wrong property names.
🔧 Debug
advanced2:00remaining
Why does this server not respond to client messages?
Look at this code snippet. The server does not send any response back to clients when they send messages. What is the cause?
Node.js
import WebSocket, { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); wss.on('connection', function connection(ws) { ws.on('message', (data) => { ws.send('Got your message'); }); }); // Client sends a message but gets no response.
Attempts:
2 left
💡 Hint
Check if the server code listens to the right events and sends messages properly.
✗ Incorrect
The server code is correct and sends a response on 'message'. If the client gets no response, it may have closed the connection or not handled the response properly.
❓ state_output
expert2:30remaining
How many clients are connected after these events?
Given this server code, how many clients are connected after two clients connect and one disconnects?
Node.js
import WebSocket, { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); let count = 0; wss.on('connection', function connection(ws) { count++; ws.on('close', () => { count--; }); }); // Two clients connect, then one disconnects.
Attempts:
2 left
💡 Hint
Count increments on connection and decrements on close.
✗ Incorrect
Two clients connect: count = 2. One disconnects: count = 1. So one client remains connected.