1. What is SSE
Server-Sent Events (SSE) is a technology that allows a server to push real-time updates to a client over a standard HTTP connection.
But SEE is one-way data stream from the server to the client. Meanwhile, WebSockets is a bidirectional protocol.
2. SSE vs WebSockets
Both are used for real-time communication.
- SEE: unidirectional, HTTP, simpler to implement.
- WebSockets: bidirectional, a new protocol based on TCP.
3. SSE use cases
- Live updates: Stock dashboards, sport scores, upload progress,…
- Notifications: sending push notifications to users
- Monitoring systems
4. Implementation
From the client, we can use the web interface EventSource to handle the incoming events or using Fetch API.
On the server, we should set the response header as below
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection": "keep-alive")
Refs:
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events