v3/examples/web-apis/eventsource/frontend/index.html
Server-Sent Events (SSE) enable servers to push data to web clients over HTTP. Unlike WebSockets, SSE is unidirectional (server to client) and works over standard HTTP.
EventSource: Checking...
SSE Endpoint URL
ConnectDisconnect
Public SSE test servers: sse.dev/test, or use the mock stream below for testing.
Since SSE requires a server, this mock simulates event streams locally for testing.
Event Interval (ms)
Event Typemessage (default)update (custom)notification (custom)random types Start Mock StreamStop Mock Stream
0
Total Events
0
Messages
0
Custom Events
0
Errors
Clear Log
--:--:--INFOReady to connect to an SSE endpoint or start mock stream...
// Create EventSource connection const source = new EventSource('https://api.example.com/stream'); // Connection opened source.onopen = (event) => { console.log('Connection opened'); }; // Default message event source.onmessage = (event) => { console.log('Message:', event.data); console.log('Last Event ID:', event.lastEventId); console.log('Origin:', event.origin); }; // Custom named events source.addEventListener('update', (event) => { console.log('Update event:', event.data); }); source.addEventListener('notification', (event) => { console.log('Notification:', event.data); }); // Error handling source.onerror = (event) => { if (source.readyState === EventSource.CONNECTING) { console.log('Reconnecting...'); } else { console.log('Error occurred'); } }; // Close connection source.close(); // Ready states EventSource.CONNECTING // 0 - Connecting EventSource.OPEN // 1 - Open EventSource.CLOSED // 2 - Closed // Server response format (text/event-stream): // data: Simple message\n\n // data: {"json": "data"}\n\n // event: custom\ndata: Custom event\n\n // id: 123\ndata: Message with ID\n\n // retry: 5000\n\n