What Is SSE?
Server-Sent Events (SSE) allow a server to push updates to the browser over a single long-lived HTTP connection.
SSE is unidirectional (server -> client only).
If the client needs to send data back, it must use a separate HTTP request.
Why SSE Was Created
Traditional HTTP:
Client → Request → Server → Response → CloseProblem:
- Server cannot push updates on its own.
- Polling solution:
- Client keeps asking.
- WebSocket solution:
- Persistent bi-directional connection
- SSE solution:
- Persistent server -> client streaming over HTTP
It is simpler than WebSockets when you only need push notifications.
How SSE Works
1 Client Opens a Connection
Browser creates an EventSourc:
const source = new EventSource("/events");This sends a regular HTTP request:
GET /events HTTP/1.1
Accept: text/event-stream2 Server Responds With Special Content-Type
Server responds:
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-aliveImportant header:
Content-Type: text/event-streamThis tells the browser:
Keep this connection open. Data will stream.
3 Server Streams Messages
Messages are sent in a special format:
data: Hello World
data: Another message
Each message ends with a blank line.
4 Browser Receives Events Automatically
Leave a comment
Your email address will not be published. Required fields are marked *


