๐
WebSocket
Bidirectional Real-time Communication
WebSocket is a protocol defined by RFC 6455 that maintains a TCP connection after the initial HTTP handshake to support bidirectional communication. The server can send data to the client first, making it ideal for real-time applications.
Architecture Diagram
๐
Client
Browser / Bot / App
โ HTTP Upgrade
→
←
โก 101 Switching
TCP Keep-Alive
←
โข Bidirectional Messages
→
โฃ Close Frame
๐ฅ๏ธ
Server
WebSocket Server
Flow Description
- Client initiates handshake with HTTP Upgrade request
- Server responds with 101 Switching Protocols, TCP connection maintained
- Bidirectional channel โ both server and client send messages freely
- Either side can close connection with Close frame
Use cases: Slack Socket Mode, Discord Gateway, real-time chat, games
How It Works
1
Client initiates WebSocket handshake with HTTP Upgrade request
2
Server responds with 101 Switching Protocols, maintaining TCP connection
3
Bidirectional channel established โ both server and client can freely send messages
4
Either side sends a Close frame to terminate the connection
Pros
- ✓ True bidirectional real-time communication
- ✓ Server can push data first
- ✓ Lightweight frames without HTTP overhead
- ✓ Works behind firewalls via outbound connections
Cons
- ✗ Connection maintenance cost (server memory/connections)
- ✗ Reconnection logic needed on disconnect
- ✗ Complex load balancer configuration
- ✗ No HTTP caching
Use Cases
Chat applications
Real-time dashboards/monitoring
Online games
Collaborative editors (Google Docs)
Real-time speech-to-text (STT โ Deepgram, Azure Speech)
Live streaming comments/reactions