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:

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:


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:


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 what https:// is to HTTP—it encrypts the WebSocket connection using TLS.


Interview Summary

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.