Server-Sent Events (SSE) is a technology that allows a client to receive real-time updates from a server via HTTP. Unlike traditional HTTP requests, SSE maintains a persistent connection, enabling the server to push data to the client as it becomes available.
Key Features
- Unidirectional Communication: Data flows only from server to client
- Event Stream Format: Uses
text/event-stream
MIME type - Automatic Reconnection: Built-in support for reconnecting after disconnection
- Cross-Origin Support: Works with CORS policies
Use Cases
- Live sports scores 🏀
- Stock market updates 📈
- Log monitoring 📜
- News feeds 📰
Implementation Example
const eventSource = new EventSource('/en/developer_docs/sse_example');
eventSource.onmessage = function(event) {
console.log('Received:', event.data);
};
Best Practices
- Use
Content-Type: text/event-stream
for event streams - Implement proper heartbeat mechanisms ❤️
- Handle connection errors gracefully ⚠️
- Use
Last-Event-ID
for resuming streams 🔁
For more technical details about SSE implementation, visit our SSE API Reference.