Intermediate LevelHTML & Semantic Web
How does the EventSource API handle real-time Server-Sent Events (SSE) in HTML?
❓ Interview Prompt: Explain EventSource initialization, text/event-stream format, automatic reconnection, and handling message events.
The 'EventSource' API provides client-side handling for Server-Sent Events (SSE), streaming real-time text updates over persistent HTTP connections.
Advantages over WebSockets:
•
Built on standard HTTP.
•
Automatic reconnection handling.
•
Built-in event IDs for resuming interrupted streams.
Code Implementation Example
html
<script>
const eventSource = new EventSource('/api/live-updates');
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received SSE update:', data);
};
</script>💡Connecting client EventSource listener to Server-Sent Events endpoint.
🎯 Interview Key Takeaway
Use EventSource for unidirectional real-time server updates with built-in automatic reconnection handling.
#SSE#EventSource#Real-Time#Intermediate
Back to all HTML & Semantic Web Questions→