Standard HTTP is a request-response protocol. The client asks, the server answers. The server cannot proactively push data to the client. But many modern applications (chat apps, live sports scores, stock tickers) need the server to send data to the client the instant it becomes available.
The client repeatedly sends HTTP requests to the server at regular intervals (e.g., every 2 seconds) asking "Any new data?". The server responds immediately with data or an empty response.
An improvement over short polling. The client sends a request to the server. Instead of responding immediately, the server holds the connection open until new data is available (or a timeout occurs). Once the server responds, the client immediately sends a new request, and the cycle repeats.
WebSockets provide a full-duplex, persistent connection between the client and server. After an initial HTTP handshake (upgrade request), the connection is "upgraded" to a WebSocket connection. Both the client and server can send messages to each other at any time over this single, long-lived TCP connection.
SSE provides a unidirectional, persistent connection from the server to the client. The client opens a connection, and the server pushes events through it whenever new data is available. The client cannot send data back over this connection.
EventSource API. Automatic reconnection.| Feature | Long Polling | WebSockets | SSE |
|---|---|---|---|
| Direction | Client-initiated | Bidirectional | Server to Client |
| Connection | Repeated HTTP | Single persistent TCP | Single persistent HTTP |
| Overhead | Medium | Very Low | Low |
| Complexity | Low | High | Medium |
| Best For | Simple notifications | Chat, gaming, trading | Live feeds, dashboards |