How do short polling, long polling, WebSockets and Server-Sent Events differ?
MediumConceptAPIs & Communication
Answer
All four deliver server-side updates to clients, but they trade simplicity against latency and overhead.
- Short polling: the client asks for updates at a fixed interval. It is simple, but it wastes requests when nothing has changed and adds up to one interval of delay.
- Long polling: the server holds the request open until data is available or a timeout occurs, and the client then reconnects immediately. Latency is lower, but every message still costs a new HTTP request.
- WebSockets: a single persistent, full-duplex TCP connection established through an HTTP upgrade; best for chat, games and collaborative editing, although long-lived connections complicate load balancing.
- Server-Sent Events: a one-way stream from server to client over plain HTTP with automatic reconnection; well suited to feeds, notifications and live scores.
More in APIs & Communication
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is a REST API, and what makes an API RESTful? | Concept | Easy | Read the answer to What is a REST API, and what makes an API RESTful? | |||
| 2 | When would you choose REST, GraphQL or gRPC? | Concept | Medium | Read the answer to When would you choose REST, GraphQL or gRPC? | |||
| 3 | What is idempotency, and how do you make a POST request safe to retry? | Concept | Medium | Read the answer to What is idempotency, and how do you make a POST request safe to retry? | |||
| 4 | How do short polling, long polling, WebSockets and Server-Sent Events differ? | Concept | Medium | Read the answer to How do short polling, long polling, WebSockets and Server-Sent Events differ? |