0
0
Node.jsframework~20 mins

WebSocket protocol concept 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
2:00remaining
What is the main advantage of WebSocket over HTTP?

WebSocket is often chosen over HTTP for certain applications. What is the main advantage of using WebSocket compared to HTTP?

AWebSocket requires no handshake, so it connects instantly without delay.
BWebSocket uses less bandwidth because it compresses all messages automatically.
CWebSocket allows full-duplex communication, enabling real-time two-way data exchange.
DWebSocket is stateless, so it does not keep any connection open between client and server.
Attempts:
2 left
💡 Hint

Think about how data flows between client and server in real-time apps like chat.

component_behavior
intermediate
2:00remaining
What happens when a WebSocket client sends a message?

In a Node.js WebSocket server, what happens immediately after a client sends a message?

Node.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', ws => {
  ws.on('message', message => {
    // What happens here?
  });
});
AThe server receives the message event and can process or respond to the client immediately.
BThe server queues the message and waits for the client to send a close signal before processing.
CThe server ignores the message unless the client sends a ping frame first.
DThe server automatically broadcasts the message to all connected clients without code.
Attempts:
2 left
💡 Hint

Consider the event listeners set up on the server side.

📝 Syntax
advanced
2:00remaining
Identify the correct way to send a message to a WebSocket client in Node.js

Which of the following code snippets correctly sends a text message 'Hello' to a connected WebSocket client?

Node.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', ws => {
  // send message here
});
Aws.send('Hello');
Bws.sendMessage('Hello');
Cws.emit('send', 'Hello');
Dws.write('Hello');
Attempts:
2 left
💡 Hint

Check the official WebSocket API method for sending data.

🔧 Debug
advanced
2:00remaining
Why does this WebSocket server code fail to respond to clients?

Consider this Node.js WebSocket server code. Why does it never send a response back to clients?

Node.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', ws => {
  ws.on('message', message => {
    ws.sendMessage('Received: ' + message);
  });
});
AThe server needs to wait for a 'ready' event before sending messages.
BThe method <code>sendMessage</code> does not exist; it should be <code>send</code>.
CThe server must use <code>ws.emit('message', ...)</code> to send data.
DThe server must call <code>ws.connect()</code> before sending messages.
Attempts:
2 left
💡 Hint

Check the WebSocket API method names carefully.

state_output
expert
2:00remaining
What is the final value of 'count' after this WebSocket server handles 3 client messages?

Given the following Node.js WebSocket server code, what is the value of count after three messages are received from any clients?

Node.js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
let count = 0;
wss.on('connection', ws => {
  ws.on('message', message => {
    count += 1;
    ws.send(`Message number ${count}`);
  });
});
ADepends on how many clients are connected
B1
C0
D3
Attempts:
2 left
💡 Hint

Count increases each time any client sends a message.