0
0
Node.jsframework~20 mins

ws library for WebSocket server in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Server 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 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}`);
  });
});
AThe client receives: "hello"
BThe client receives: "Received: hello"
CThe client receives: "message: hello"
DThe client receives nothing
Attempts:
2 left
💡 Hint
Look at what the server sends back inside the message event handler.
lifecycle
intermediate
1: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');
  });
});
A'close'
B'disconnect'
C'end'
D'exit'
Attempts:
2 left
💡 Hint
Think about the standard WebSocket event for connection closure.
📝 Syntax
advanced
1: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.
Aconst wss = new WebSocketServer({ listen: 3000 });
Bconst wss = new WebSocket.Server(3000);
Cconst wss = new WebSocketServer(port=3000);
Dconst wss = new WebSocketServer({ port: 3000 });
Attempts:
2 left
💡 Hint
Check the correct way to pass options to the WebSocketServer constructor.
🔧 Debug
advanced
2: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.
AThe server code is correct; the client likely closed the connection before response.
BThe server does not listen for 'message' events correctly because the callback uses arrow function.
CThe server is missing the 'open' event listener to start sending messages.
DThe server is missing the 'connection' event listener on the WebSocketServer instance.
Attempts:
2 left
💡 Hint
Check if the server code listens to the right events and sends messages properly.
state_output
expert
2: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.
A0
B2
C1
D3
Attempts:
2 left
💡 Hint
Count increments on connection and decrements on close.