0
0
Node.jsframework~20 mins

Server-Sent Events alternative in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1: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?
AWebSocket allows two-way communication between client and server, unlike SSE which is one-way from server to client.
BWebSocket only supports text data, while SSE supports binary data.
CWebSocket requires less server resources because it uses HTTP/2, while SSE uses HTTP/1.1.
DWebSocket automatically retries connections without any client-side code, unlike SSE.
Attempts:
2 left
💡 Hint
Think about the direction of data flow and interaction between client and server.
component_behavior
intermediate
1: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}`);
  });
});
AThe server will broadcast the received message to all connected clients.
BThe server will echo back the received message prefixed with 'Server received: ' to the same client.
CThe server will close the connection after receiving the message.
DThe server will ignore the message and not respond.
Attempts:
2 left
💡 Hint
Look at the ws.send call inside the message event handler.
📝 Syntax
advanced
2: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?
A
const ws = new WebSocket('ws://localhost:8080');
ws.on('message', function(data) { console.log(data); });
B
const ws = new WebSocket('ws://localhost:8080');
ws.addEventListener('message', event => console.log(event.data));
C
import WebSocket from 'ws';
const ws = new WebSocket('http://localhost:8080');
ws.onmessage = data => console.log(data);
D
import WebSocket from 'ws';
const ws = new WebSocket('ws://localhost:8080');
ws.on('message', data => console.log(`Received: ${data}`));
Attempts:
2 left
💡 Hint
Consider the correct import and event listener syntax for Node.js WebSocket client.
🔧 Debug
advanced
2: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!');
});
AThe server is listening on port 3000 but clients are connecting to port 8080.
BThe WebSocketServer constructor requires a 'host' option to accept connections.
CThe server must call wss.listen() to start listening for connections.
DThe ws.send call should be inside a 'message' event handler.
Attempts:
2 left
💡 Hint
Check if the client connection URL matches the server port.
state_output
expert
2: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}`);
      }
    });
  });
});
A
Client connected
Received: Hello
Client connected
B
Received: Hello
Client connected
Client connected
C
Client connected
Client connected
Received: Hello
D
Client connected
Client connected
Received: Broadcast: Hello
Attempts:
2 left
💡 Hint
Consider the order of events when clients connect and send messages.