1. Why HTTP Isn't Enough
The Problem
HTTP follows a Request → Response model.
Client --------Request--------> Server
Client <-------Response-------- Server
After the response is sent:
- Connection is usually closed (or kept alive for reuse, but idle).
- The server cannot send data on its own.
- If the client wants new information, it must ask again.
Example:
A stock price changes.
Server: "Stock is ₹120."
After 2 seconds:
Stock becomes ₹125.
Can the server notify the client?
❌ No.
The client has to keep asking:
GET /stock
GET /stock
GET /stock
GET /stock
This is called Polling.
Problems with polling:
- Many unnecessary requests.
- Higher server load.
- Wasted bandwidth.
- Updates are delayed depending on the polling interval.
Real Example
Imagine WhatsApp.
You receive:
Hi!
With HTTP only:
Phone:
Any new messages?
No.
Any new messages?
No.
Any new messages?
No.
Any new messages?
Yes!
That would be inefficient.
Instead, WhatsApp needs the server to instantly push the message.
2. Full Duplex Communication
This is where WebSockets come in.
A WebSocket creates a persistent connection between the client and server.
Client <==================> Server
Both sides can send messages at any time.
This is called Full Duplex Communication.
Unlike HTTP:
Request
↓
Response
(wait)
New Request
↓
New Response
WebSocket works like:
Client ---------------------- Server
Client ---> Server
Server ---> Client
Client ---> Server
Server ---> Client
Nobody has to wait for a request first.
Half Duplex vs Full Duplex
Half Duplex
Only one side talks at a time.
Example:
Walkie-talkie
You:
"Hello"
(wait)
Friend:
"Hi"
(wait)
Full Duplex
Both sides can talk simultaneously.
Example:
Phone call
You: "Hello"
Friend: "Yes?"
You interrupt
Friend interrupts
Both speak anytime
WebSockets work like a phone call.
3. WebSocket Handshake
WebSockets don't replace HTTP completely.
They start as an HTTP request.
Step 1
Browser sends a normal HTTP request.
GET /chat HTTP/1.1
Upgrade: websocket
Connection: Upgrade
The client is essentially saying:
"Let's switch this HTTP connection into a WebSocket connection."
Step 2
Server agrees.
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
101 Switching Protocols means:
"Okay, we're no longer using HTTP on this connection. We'll now communicate using the WebSocket protocol."
Step 3
The connection stays open.
Client <=====================> Server
Now either side can send messages whenever needed.
Visual Flow
Browser
│
│ HTTP GET
│ Upgrade: websocket
▼
Server
│
│ 101 Switching Protocols
▼
═══════════════════════════════
Persistent WebSocket Connection
Client <===================> Server
Messages
Messages
Messages
Messages
4. Real-Time Applications
WebSockets are ideal when data changes frequently and users should see updates immediately.
Examples include:
- 💬 Chat applications (WhatsApp, Slack, Discord)
- 📈 Live stock prices
- 🪙 Cryptocurrency exchanges
- 🎮 Multiplayer games
- 🚗 Ride tracking (Uber, Ola)
- 📍 Live GPS/location tracking
- ⚽ Live sports scores
- 📊 Real-time dashboards and monitoring
- 🔔 Instant notifications
HTTP vs WebSocket
| Feature | HTTP | WebSocket |
| Communication | Request → Response | Two-way communication |
| Connection | Short-lived (or reused but request-based) | Persistent |
| Server can send data anytime | ❌ No | ✅ Yes |
| Best for | REST APIs, websites, file uploads | Chats, live updates, gaming, streaming |
| Protocol | HTTP/HTTPS | WebSocket (`ws://` or secure `wss://`) |
Note:
wss://is to WebSocket whathttps://is to HTTP—it encrypts the WebSocket connection using TLS.
Interview Summary
- HTTP is request-response based, so the server cannot initiate communication with the client.
- Polling can simulate real-time updates but is inefficient because it generates many unnecessary requests.
- WebSocket solves this by creating a persistent, full-duplex connection where both client and server can send messages at any time.
- A WebSocket connection begins with an HTTP handshake using the
Upgrade: websocketheader. If accepted, the server responds with 101 Switching Protocols, and communication continues using the WebSocket protocol. - WebSockets are commonly used for chat applications, live notifications, multiplayer games, stock tickers, GPS tracking, and other real-time systems.
This level of understanding is sufficient for backend interviews.
Later, when you work with Node.js, you'll see how libraries like ws or Socket.IO implement these concepts in practice.