🔹 What is WebSocket?
WebSocket is a communication protocol.
It provides full-duplex (two-way) communication between client (browser/app) and server.
Unlike HTTP (request/response), WebSocket allows continuous data exchange once the connection is established.
✅ Real-time communication
✅ Lower latency than HTTP
✅ Less overhead (no repeated HTTP handshakes)
✅ Useful for live applications
Client → sends an HTTP request with Upgrade: websocket header.
Server → responds with Switching Protocols (101).
Now, the connection is upgraded to WebSocket.
Both client & server keep the channel open.
Either side can send messages at any time.
Data is sent in small frames (text or binary).
Lightweight → reduces bandwidth.
HTTP: 📩 Postman → knocks → delivers → leaves → comes again for next message.
WebSocket: 📞 Phone Call → once connected, you talk freely until you hang up.
🌍 Chat Apps – WhatsApp, Messenger
📈 Stock Market Tickers – Live stock updates
🎮 Gaming – Multiplayer real-time sync
🚖 Ride-Hailing Apps – Live driver tracking
🔔 Notifications – Instant alerts
Feature | HTTP | WebSocket |
---|---|---|
Connection | Request/Response | Persistent |
Direction | One-way | Two-way |
Latency | Higher | Low |
Overhead | More (headers each time) | Minimal |
Best For | Static data fetch | Real-time apps |
Client (Browser) 🖥️
⬇️ (Handshake over HTTP)
Server 🌐
⬆️ (Upgrade response)
🔗 Connection established!
➡️ Client sends messages (Chat, Game Move, Order)
⬅️ Server sends updates (Reply, Stock Prices, Notifications)