Server-Sent Events (EventSource) Demo

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...

Connection

Public SSE test servers: sse.dev/test, or use the mock stream below for testing.

Mock Event Stream

Local Mock (No Server Required)

Since SSE requires a server, this mock simulates event streams locally for testing.

Statistics

0
Total Events
0
Messages
0
Custom Events
0
Errors

Event Log

--:--:-- INFO Ready to connect to an SSE endpoint or start mock stream...

EventSource API Reference

// 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