0% completed
Difference Between Long-Polling, WebSockets, and Server-Sent Events
On This Page
Key Differences
How to Choose
Quick Reference
Conclusion
Long-Polling, WebSockets, and Server-Sent Events all solve the same problem: getting updates from the server to the client without refreshing the page. They just solve it in three different ways. Here is each one in a single sentence:
- Long-Polling: the client asks, and the server holds the request open until it has data to send. After every answer, the client asks again.
- WebSocket: one persistent connection where both the client and the server can send messages at any time.
- Server-Sent Events (SSE): one persistent HTTP connection where the server streams updates down to the client, one way only.
Key Differences
| Feature | Long-Polling | WebSocket | Server-Sent Events |
|---|---|---|---|
| Direction | Client asks, server answers | Two-way, both sides anytime | One-way, server to client |
| Connection | New request after every message | One persistent connection | One persistent connection |
| Built on | Plain HTTP | Starts as HTTP, then upgrades to its own protocol | Plain HTTP |
| Reconnection | Client re-requests after every response or timeout | You write the reconnect logic yourself | Browser reconnects automatically |
| Overhead per message | Full HTTP request every time | Very small | Small |
| Best for | Fallback, simple notifications | Chat, games, collaboration | Feeds, scores, streaming AI answers |
How to Choose
Ask three questions, in order:
- Do the client and the server both need to send messages at any time? If yes, use WebSocket. Nothing else gives you a true two-way channel.
- Do updates only flow from the server to the client? If yes, use Server-Sent Events. It is simpler than WebSocket, it is plain HTTP, and the browser handles reconnection for you.
- Do you need something that works through old proxies and strict corporate networks, or a fallback when the other two fail? Use Long-Polling. It is the least efficient, but it works almost everywhere.
💡 In a system design interview, do not just name a technique. Say why it fits. "Chat needs messages in both directions with low latency, so I would use WebSockets, with long-polling as a fallback for restrictive networks." For a notifications feed: "Updates only flow to the client, so SSE gives me streaming over plain HTTP, and the browser reconnects on its own." Naming the trade-off is what earns the credit.
Quick Reference
-
Long-Polling
- Connection: a new HTTP request after every message
- Direction: client asks, server answers when it has data
- Reconnection: the client re-requests every time
- Use it for: fallbacks and simple notification delivery
-
WebSocket
- Connection: one persistent, upgraded connection
- Direction: both ways, at any time
- Reconnection: your application handles it
- Use it for: chat, multiplayer games, collaborative editing, trading
-
Server-Sent Events
- Connection: one persistent HTTP connection
- Direction: server to client only
- Reconnection: automatic, built into the browser
- Use it for: live feeds, scores, prices, progress, streaming AI answers
Conclusion
There is no single winner. Long-Polling is the most compatible, WebSocket is the most capable, and Server-Sent Events is the simplest for one-way streams. Match the technique to the direction of your data, and you will get the right answer in both production and interviews.
Vu Tong
· 4 months ago
It's worth to mention that to avoid performance impact, the server should have some kind of asynchronous processing for incoming requests. If the server uses a traditional thread-per-request model and every client holds a long-lived connection, that will hurt the overall performance.
dominikcubic
· 3 years ago
In this description of AJAX polling, is the "requested webpage" the same as the "Client"?
- Client requests data from a server using regular HTTP.
- The requested webpage opens a connection to the server.
- The server sends the data to the client whenever there's new information available.
heybenross
· 3 years ago
When should each be used? What are the pros and cons of each?
Reading Progress
0%
On This Page
Key Differences
How to Choose
Quick Reference
Conclusion