Intro
WebSockets are a protocol that enables full duplex, bidirectional communication between a client and a server over a single TCP connection. This technology is crucial for applications requiring real-time updates and interactive features.
Use Cases
- Real-time chat applications
- Live sports scores and updates
- Collaborative editing tools
- Financial trading platforms (Binance)
- Online gaming
- …etc
Key Concepts
- Full-duplex Communication: Unlike HTTP, WebSockets allow simultaneous two-way communication.
- Persistent Connection: Once established, the connection remains open, reducing latency for subsequent messages.
- Lightweight: WebSocket messages have minimal overhead compared to HTTP requests.
- Cross-origin Communication: WebSockets can connect to different domains, following security policies.
Lifecycle
- Handshake: The client initiated it using an HTTP upgrade request.
- Connection Established: The server accepts, and the protocol switches to WebSocket.
- Data Transfer: Bidirectional message exchange.
- Connection Closure: Either party can initiate closure.
Detailed breakdown