WebSockets
A communication protocol that provides full-duplex, bidirectional communication channels over a single TCP connection for real-time data transfer.
Detailed Explanation
HTTP is request-response: the client asks, the server responds, and the connection closes. WebSockets maintain a persistent connection where either side can send data at any time. After an initial HTTP handshake upgrades to a WebSocket connection, both client and server can push messages without the client requesting them.
WebSockets are essential for real-time features: chat applications, live notifications, collaborative editing, multiplayer gaming, live dashboards, and stock tickers. They provide lower latency than HTTP long-polling and are more efficient than Server-Sent Events for bidirectional communication.
Why It Matters
WebSockets enable real-time features that are impossible with traditional HTTP. They are the standard for any application requiring live, bidirectional communication.
Real-World Example
Google Docs uses WebSockets to sync edits between collaborators in real-time. When you type, your changes are instantly sent to other editors through the WebSocket connection.
When to Use
For real-time features: chat, notifications, live updates, collaborative editing, gaming, and live dashboards. Use HTTP for standard request-response patterns.
Advantages
- Full-duplex bidirectional communication
- Lower latency than HTTP polling
- Efficient: persistent connection reduces overhead
- Works through most proxies and firewalls
- Native browser support (WebSocket API)
Disadvantages
- Stateful connections are harder to scale
- More complex than HTTP request-response
- Connection management overhead
- Not all load balancers support WebSockets well
- Reconnection logic required for reliability
Related Terms
Frequently Asked Questions
What is the difference between WebSockets and HTTP?
HTTP is request-response (client initiates, server responds, connection closes). WebSockets maintain a persistent connection where either side can send data at any time. HTTP is for standard web requests; WebSockets are for real-time communication.
What is the difference between WebSockets and SSE?
WebSockets are bidirectional (client and server can send). SSE (Server-Sent Events) are unidirectional (server pushes to client). SSE is simpler for one-way real-time updates (notifications, live feeds). WebSockets are needed for chat and collaboration.
How do WebSockets scale?
WebSockets are stateful (each connection is a persistent session). Scale with sticky sessions (same client to same server), a message broker (Redis pub/sub) for broadcasting, or managed WebSocket services (Ably, Pusher, Socket.io).
Do WebSockets work through proxies?
Most modern proxies support WebSockets. However, some corporate proxies and older load balancers may not. Use WSS (WebSocket Secure, port 443) which works through most proxies since it uses the standard HTTPS port.
What is Socket.io?
Socket.io is a JavaScript library that adds real-time capabilities on top of WebSockets. It provides fallbacks (long-polling), automatic reconnection, rooms, and broadcasting. It simplifies WebSocket development but adds overhead.