Server sent events
I recently had a good discussion with one of my co-worker around a high-level design problem where we were discussing how to choose between Server sent events vs short polling, and I thought to publish our excalidraw screenshot here.
Server sent events are uni-directional communication channel where the server sends updates to the client over an open HTTP connection. Use cases like real-time stock price updates, live sports scores can be handled well with them.
Clients start an SSE connection through a request to the server’s supported SSE endpoint, e.g.:
GET /live_scores
Accept: text/event-stream
Connection: keep-alive
Like WebSockets, SSE also uses an event-based API — the EventSource API — for implementation. It uses a special Content-Type of text/event-stream. The client-side code to receive the server’s updates is nearly identical to WebSockets — with the exception that SSE does not allow broadcasting back. The server-side code handles the complexity of streaming events to the client’s front-end.
This W3School tutorial provides snippets on both sides.