Receive Server-Sent Events
Use EventSource when a server only needs to push incremental updates to the page. Listen for messages and errors, then close the connection when leaving.
Complete the Example
Use EventSource when a server only needs to push incremental updates to the page. Listen for messages and errors, then close the connection when leaving.
const source = new EventSource('https://api.example.com/agent/stream');
source.addEventListener('message', (event) => {
console.log('Update:', event.data);
});
source.addEventListener('error', (error) => {
console.error('Stream failed', error);
});
// Call source.close() when the page no longer needs updates.
SSE is one-way. Choose WebSocket for bidirectional communication. See Event Source.