What are WebSockets and what problems do they solve?
Learn what WebSockets are, how they enable real-time full-duplex communication, and the polling problems they solve, with examples and interview answers.
Expected Interview Answer
WebSockets are a protocol that opens a single, long-lived, full-duplex TCP connection between a client and server, letting both sides send messages at any time without repeatedly opening new connections.
They solve the real-time communication problem that plain HTTP struggles with. HTTP is request-response: the client must ask before the server can answer, so pushing live data means wasteful polling or clumsy long-polling. WebSockets keep one connection open after an initial HTTP handshake upgrade, so the server can push updates the instant they happen with very low overhead per message.
- True real-time, bidirectional messaging
- Low latency after the connection is established
- Far less overhead than repeated HTTP polling
- Server can push data without a client request
- Efficient for chat, live feeds, games and dashboards
AI Mentor Explanation
Polling is like a spectator phoning the stadium after every ball to ask the latest score, hanging up, then calling again. A WebSocket is a dedicated commentary line kept open all match: the moment a wicket falls or a six is hit, the commentator speaks and you hear it instantly, and you can shout questions back down the same line without ever redialling.
Step-by-Step Explanation
Step 1
Identify the need
Recognise that the app needs instant, two-way updates that HTTP request-response cannot deliver efficiently.
Step 2
Open with a handshake
The client sends an HTTP request with an Upgrade header; the server agrees and switches the connection to the WebSocket protocol.
Step 3
Keep the connection alive
A single TCP connection stays open, avoiding repeated connection setup and teardown costs.
Step 4
Exchange messages freely
Either side sends framed messages at any time in full-duplex, with tiny per-message overhead.
Step 5
Close gracefully
Either party sends a close frame and the connection shuts down cleanly when real-time updates are no longer needed.
What Interviewer Expects
- Definition of full-duplex, persistent connection
- Contrast with HTTP polling and long-polling
- Awareness of the initial HTTP handshake upgrade
- Real use cases like chat, live feeds and games
- Understanding of latency and overhead trade-offs
Common Mistakes
- Calling WebSockets just faster HTTP
- Forgetting the connection is bidirectional
- Ignoring that it starts as an HTTP handshake
- Claiming WebSockets replace HTTP for everything
- Not mentioning any real-time use case
Best Answer (HR Friendly)
“WebSockets keep an always-open connection between the browser and the server so both sides can send messages the instant they have news, instead of the browser constantly asking. That is why chat apps and live dashboards feel instant rather than laggy.”
Code Example
const socket = new WebSocket('wss://example.com/live')
socket.addEventListener('open', () => {
socket.send(JSON.stringify({ type: 'subscribe', channel: 'prices' }))
})
socket.addEventListener('message', (event) => {
const data = JSON.parse(event.data)
console.log('Live update:', data)
})
socket.addEventListener('close', () => {
console.log('Connection closed')
})Follow-up Questions
- How do WebSockets differ from HTTP?
- What is Server-Sent Events and when would you use it instead?
- How do you scale WebSocket connections across many servers?
- How do you handle reconnection and dropped connections?
- What security concerns apply to WebSocket connections?
MCQ Practice
1. WebSockets provide what kind of communication channel?
WebSockets are full-duplex, so both client and server can send messages independently at any time over one connection.
2. What is the main inefficiency of HTTP polling that WebSockets solve?
Polling repeatedly opens connections and re-sends headers to check for updates, wasting bandwidth; WebSockets keep one open connection.
3. Which use case is the best fit for WebSockets?
Live chat needs instant two-way messaging, which is exactly what a persistent full-duplex WebSocket connection is built for.
Flash Cards
What is a WebSocket? — A protocol giving a single persistent full-duplex TCP connection between client and server for real-time messaging.
What problem do WebSockets solve? — They remove the need for wasteful HTTP polling by letting the server push updates instantly over one open connection.
Full-duplex means? — Both sides can send and receive messages independently at the same time.
Name three WebSocket use cases. — Chat apps, live dashboards/feeds, and multiplayer games.