Abstract
- The server and client exchange data instantly (or near-instantly) as soon as something happens, instead of waiting for the client to refresh or poll on a schedule
๐ฆ๐ฒ๐ฟ๐๐ฒ๐ฟ-๐ฆ๐ฒ๐ป๐ ๐๐๐ฒ๐ป๐๐
- Based HTTP, server keeps a connection open and pushes text data to the client whenever it wants. It is like a one-way webSocket
Workflow
- Client opens connection:
new EventSource('/events')
- Server responds with
Content-Type: text/event-stream
and keeps connection alive- Server pushes data in simple format:
data: {"price": 142.50, "symbol": "AAPL"}
- Client receives events via
eventSource.onmessage = (event) => { ... }
Doesn't guaranteed delivery
This is very bad for chat apps or collaborative editing.
However, it still works well for stock tickers and progress update where missing one score update doesnโt matter as long as the latest value is displayed.
What if client wants to change things mid-stream?
It must use a separate HTTP request to adjust the behaviour. Thatโs why for bidirectional control, people often pick WebSockets (client โ server messages on the same channel).