WebSocket is a communication protocol that allows a two-way, full-duplex connection between a client and a server over a single, long-lived TCP connection.
Click to reveal answer
beginner
How does WebSocket differ from HTTP?
Unlike HTTP, which is request-response and short-lived, WebSocket keeps the connection open, allowing real-time, continuous data exchange without repeated handshakes.
Click to reveal answer
intermediate
What is the initial step to establish a WebSocket connection?
The client sends an HTTP upgrade request to the server asking to switch the protocol from HTTP to WebSocket, called the handshake.
Click to reveal answer
beginner
What does 'full-duplex' mean in WebSocket communication?
Full-duplex means both client and server can send and receive messages independently at the same time without waiting for the other side.
Click to reveal answer
beginner
Why is WebSocket useful for real-time applications?
Because it keeps the connection open and allows instant two-way data flow, WebSocket is great for chat apps, live updates, games, and other apps needing fast interaction.
Click to reveal answer
What protocol does WebSocket start with before switching?
AFTP
BTCP
CSMTP
DHTTP
✗ Incorrect
WebSocket begins with an HTTP handshake to upgrade the connection to WebSocket.
Which feature best describes WebSocket communication?
ARequest-response only
BFull-duplex
COne-way streaming
DStateless
✗ Incorrect
WebSocket supports full-duplex communication, meaning both sides can send data simultaneously.
What happens after the WebSocket handshake is successful?
AA persistent connection is established
BClient sends HTTP requests repeatedly
CConnection closes immediately
DServer sends a file download
✗ Incorrect
After handshake, a persistent, open connection allows continuous data exchange.
Which is NOT a typical use case for WebSocket?
AStatic website hosting
BReal-time gaming
CLive chat applications
DStock price updates
✗ Incorrect
Static websites do not require real-time two-way communication, so WebSocket is unnecessary.
What does the WebSocket protocol use to keep the connection alive?
ARepeated HTTP requests
BMultiple UDP packets
CA single TCP connection
DEmail notifications
✗ Incorrect
WebSocket uses a single TCP connection kept open for continuous communication.
Explain how a WebSocket connection is established and why it is different from a normal HTTP connection.
Think about the initial request and what changes after the handshake.
You got /4 concepts.
Describe real-life scenarios where WebSocket is a better choice than HTTP and why.
Consider apps that need instant updates without refreshing.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the WebSocket protocol in Node.js applications?
easy
A. To send HTTP requests from client to server
B. To create a live, two-way communication channel between client and server
C. To serve static files like HTML and CSS
D. To store data permanently on the server
Solution
Step 1: Understand WebSocket communication
WebSocket allows a persistent connection where both client and server can send messages anytime.
Step 2: Compare with other options
HTTP requests are one-way and short-lived, static files serving and data storage are unrelated to WebSocket's main role.
Final Answer:
To create a live, two-way communication channel between client and server -> Option B
Quick Check:
WebSocket = two-way live connection [OK]
Hint: WebSocket means live two-way chat between client and server [OK]
Common Mistakes:
Confusing WebSocket with HTTP request-response
Thinking WebSocket serves static files
Assuming WebSocket stores data permanently
2. Which of the following is the correct way to create a WebSocket server using the 'ws' library in Node.js?
easy
A. const ws = new WebSocket(8080);
B. const ws = WebSocket.createServer(8080);
C. const ws = new WebSocket.Server({ port: 8080 });
D. const ws = WebSocket.listen(8080);
Solution
Step 1: Recall 'ws' library syntax
The 'ws' library creates a server with new WebSocket.Server and a config object with port.
Step 2: Check each option
Only const ws = new WebSocket.Server({ port: 8080 }); uses correct constructor and port option. Others use invalid methods or constructors.
Final Answer:
const ws = new WebSocket.Server({ port: 8080 }); -> Option C
Quick Check:
WebSocket.Server + port config = correct [OK]
Hint: Use new WebSocket.Server({ port }) to create server [OK]
Common Mistakes:
Using WebSocket constructor instead of WebSocket.Server
Calling non-existent methods like createServer or listen
Passing port as a direct argument instead of in an object
3. Given this Node.js WebSocket server code snippet, what will be logged when a client sends the message 'hello'?
B. WebSocket.Server should be WebSocket.createServer
C. The 'message' event handler should be 'onMessage'
D. msg.toUpperCase should be called as a function: msg.toUpperCase()
Solution
Step 1: Check usage of toUpperCase
toUpperCase is a function and must be called with parentheses to get the uppercase string.
Step 2: Verify other parts
WebSocket.Server is correct, 'message' event is correct, port can be a number.
Final Answer:
msg.toUpperCase should be called as a function: msg.toUpperCase() -> Option D
Quick Check:
Call string methods with () [OK]
Hint: String methods need () to execute, not just reference [OK]
Common Mistakes:
Forgetting parentheses on string methods
Using wrong event names like 'onMessage'
Confusing WebSocket server creation syntax
5. You want to build a Node.js WebSocket server that broadcasts any message received from one client to all connected clients except the sender. Which code snippet correctly implements this behavior?