Server-Sent Events for One-Way Streaming
Use EventSource to receive a stream of server-sent events, parse event types and data, and reconnect automatically on network interruptions.
What Are Server-Sent Events?
SSE is a standard HTTP-based protocol for the server to push events to the browser over a single long-lived connection. One-way: server → client only. Simpler than WebSockets when you don't need to send back.
EventSource API
The browser's EventSource connects to a URL and listens for events. Standard, built-in, no library needed.
const es = new EventSource('/api/events');
es.addEventListener('message', (event) => {
console.log('Received:', event.data);
});
es.addEventListener('error', () => {
console.error('Connection error');
});All lessons in this course
- WebSocket API: open message close error
- Socket.io Client Integration
- Server-Sent Events for One-Way Streaming
- Real-time UI Patterns