WebSocket is a protocol used for real-time communication between a client and a server. Unlike normal HTTP communication, WebSocket allows continuous, two-way communication over a single connection.
1. WebSocket Connection is Initiated by the Client
A WebSocket connection always starts from the client side (browser, mobile app, or any client application).
The client sends a request to the server asking to upgrade the connection to WebSocket.
Example HTTP request headers:
GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
If the server supports WebSocket, it responds with:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
This process is called the WebSocket Handshake.
After this handshake, the protocol switches from HTTP to WebSocket.
2. WebSocket is Bi-Directional
In traditional HTTP communication:
Client → Request
Server → Response
The server cannot send data unless the client first sends a request.
But in WebSocket:
Client ⇄ Server
Both the client and the server can send messages at any time without waiting for a request.
This makes WebSocket ideal for real-time applications.
Examples:
- Chat applications
- Online gaming
- Live notifications
- Stock price updates
- Real-time dashboards
3. WebSocket Connection is Persistent
A WebSocket connection remains open for a long time.
Unlike HTTP where the connection is closed after each response, WebSocket maintains a persistent connection.
This allows continuous communication like:
Client ⇄ Server
Client ⇄ Server
Client ⇄ Server
Because the connection stays open, it reduces the overhead of repeatedly creating new HTTP connections.
4. WebSocket Works Through Firewalls
Many networks use firewalls that restrict unknown ports.
However, WebSocket usually runs on the same ports used by HTTP and HTTPS:
-
Port 80 →
ws://(WebSocket) -
Port 443 →
wss://(Secure WebSocket)
Since these ports are commonly allowed by firewalls, WebSocket connections generally work without issues.
5. Why WebSocket is Important
WebSocket is widely used in modern applications because it enables low-latency and efficient communication.
Benefits include:
- Real-time data updates
- Lower network overhead
- Persistent connection
- Full duplex communication (two-way communication)
Conclusion
A WebSocket connection:
- Is initiated by the client
- Starts as an HTTP connection
- Uses a handshake to upgrade to WebSocket
- Becomes a persistent connection
- Allows bi-directional communication
- Works easily through firewalls using ports 80 and 443
Because of these features, WebSocket is one of the most important technologies used for building real-time web applications.

Top comments (0)