A WebSocket is a communication protocol that provides a full-duplex, persistent connection between a client and a server, allowing real-time data exchange.
Click to reveal answer
beginner
How does WebSocket differ from HTTP?
Unlike HTTP, which is request-response and stateless, WebSocket creates a continuous connection allowing both client and server to send data anytime without repeated handshakes.
Click to reveal answer
beginner
What is the main advantage of using WebSockets for real-time communication?
WebSockets reduce latency and overhead by keeping the connection open, enabling instant data transfer which is ideal for chat apps, live updates, and games.
Click to reveal answer
intermediate
What does 'full-duplex' mean in the context of WebSockets?
Full-duplex means data can be sent and received simultaneously between client and server over the same connection.
Click to reveal answer
intermediate
What is the initial step to establish a WebSocket connection?
The client sends a WebSocket handshake request to the server over HTTP, which the server upgrades to a WebSocket connection if accepted.
Click to reveal answer
Which protocol does WebSocket start with before upgrading the connection?
ASMTP
BHTTP
CFTP
DTCP only
✗ Incorrect
WebSocket connections begin with an HTTP handshake request that upgrades the connection to WebSocket.
What type of communication does WebSocket provide?
ABroadcast only
BHalf-duplex
CSimplex
DFull-duplex
✗ Incorrect
WebSocket supports full-duplex communication, allowing simultaneous two-way data flow.
Which of these is a common use case for WebSockets?
ALive chat applications
BStatic web pages
CEmail sending
DFile storage
✗ Incorrect
Live chat apps need real-time updates, which WebSockets provide efficiently.
What happens if a WebSocket connection is closed?
AConnection switches to HTTP automatically
BData continues to flow automatically
CClient and server must re-establish connection to communicate again
DServer sends data without client request
✗ Incorrect
Once closed, the WebSocket connection must be reopened to resume communication.
Which layer of the internet protocol stack does WebSocket operate on?
AApplication layer
BTransport layer
CNetwork layer
DData link layer
✗ Incorrect
WebSocket is an application layer protocol built on top of TCP.
Explain how WebSockets enable real-time communication between a client and a server.
Think about how WebSocket keeps the connection open and allows both sides to send data anytime.
You got /4 concepts.
Describe a real-life example where WebSockets improve user experience compared to traditional HTTP.
Consider apps like messaging or live sports scores.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using WebSockets for communication between a client and a server?
easy
A. They only allow one-way communication from server to client.
B. They allow real-time, two-way communication without repeatedly opening new connections.
C. They require a new connection for every message sent.
D. They work only for static web pages without interaction.
Solution
Step 1: Understand WebSocket communication
WebSockets keep a single connection open for continuous two-way data exchange.
Step 2: Compare with other methods
Unlike HTTP requests, WebSockets avoid opening new connections for each message.
Final Answer:
They allow real-time, two-way communication without repeatedly opening new connections. -> Option B
Quick Check:
Two-way real-time communication = They allow real-time, two-way communication without repeatedly opening new connections. [OK]
Hint: WebSockets keep one open connection for instant two-way chat [OK]
Common Mistakes:
Thinking WebSockets only send data one way
Confusing WebSockets with HTTP polling
Assuming new connection per message
2. Which of the following is the correct way to create a WebSocket connection in JavaScript?
easy
A. const socket = new WebSocket('http://example.com');
B. const socket = WebSocket.connect('ws://example.com');
C. const socket = WebSocket('https://example.com');
D. const socket = new WebSocket('ws://example.com');
Solution
Step 1: Identify correct WebSocket URL scheme
WebSocket URLs start with ws:// or wss:// for secure connections.
Step 2: Check JavaScript syntax
The correct syntax uses new WebSocket(url) with the ws:// scheme.
Final Answer:
const socket = new WebSocket('ws://example.com'); -> Option D
Quick Check:
WebSocket URL starts with ws:// = const socket = new WebSocket('ws://example.com'); [OK]
Hint: WebSocket URLs start with ws:// or wss:// [OK]
Common Mistakes:
Using http:// or https:// instead of ws://
Calling WebSocket without new keyword
Using incorrect method like WebSocket.connect
3. Consider this JavaScript code snippet using WebSocket: