๐จ
SSE (Server-Sent Events)
ServerโClient Unidirectional Stream
SSE is an HTTP-based unidirectional streaming technology. The server can continuously send events to the client, but client-to-server communication requires separate HTTP requests. It is simpler than WebSocket, compatible with HTTP/2, and has automatic reconnection built into browsers.
Architecture Diagram
๐
Client
EventSource API
โ GET Request
→
HTTP Keep-Alive
←
โก text/event-stream
←
data: {...}
←
data: {...}
Unidirectional stream (Server → Client)
๐ฅ๏ธ
Server
Content-Type: text/event-stream
On disconnect, the browser auto-reconnects (Last-Event-ID sent)
Flow Description
- Client sends HTTP GET request to server via EventSource API
- Server responds with text/event-stream, keeps connection
- Server streams events in
data:format - Browser auto-reconnects on disconnect
How It Works
1
Client sends HTTP GET request to server via EventSource API
2
Server responds with Content-Type: text/event-stream, maintaining connection
3
Server streams in data: format when events occur
4
Browser auto-reconnects on disconnect (using Last-Event-ID)
Pros
- ✓ Simple implementation based on HTTP
- ✓ Built-in automatic reconnection
- ✓ Leverages HTTP/2 multiplexing
- ✓ Text-based, easy to debug
Cons
- ✗ Unidirectional only (serverโclient)
- ✗ Inefficient for binary data transfer
- ✗ Not supported in IE/older browsers
- ✗ Max concurrent connection limit (6 in HTTP/1.1)
Use Cases
Real-time notifications/feeds
AI chatbot streaming responses (ChatGPT)
Stock price updates
Build/deploy log streaming
Live scoreboards