How do Server-Sent Events (SSE) differ from WebSockets, and when would you choose them?
Short answer
SSE (via EventSource) provides a one-way stream of text events FROM server TO client over a single long-lived HTTP connection, automatically reconnecting on disconnect — simpler than WebSockets when you don't need the client to send messages back over the same connection, like…
Advertisement
AModel answer
SSE (via EventSource) provides a one-way stream of text events FROM server TO client over a single long-lived HTTP connection, automatically reconnecting on disconnect — simpler than WebSockets when you don't need the client to send messages back over the same connection, like live notifications or a streaming AI chat response.
const events = new EventSource('/api/notifications');
events.onmessage = (e) => console.log('New notification:', e.data);Real-world example
Streaming a live AI-generated response token-by-token to the client, or pushing live notification counts.
Likely follow-ups
- Why can't SSE send binary data the way WebSockets can?
Related topics