
Server-Sent Events
Stack Patterns — Episode 09 Last episode built a WebSocket. Full duplex. Both sides talk. Splendid for chat. Rather generous for a notification badge. Most real-time features are one-way: the server knows something changed, the client needs to hear about it. Dashboards, feeds, build logs, stock tickers. The client never sends. It listens. The browser solved this in 2015 with one line of JavaScript that nobody teaches. The Client const source = new EventSource ( ' /events ' ); source . onmessage = e => document . querySelector ( ' #feed ' ). textContent = e . data ; One line to connect. One line to handle. No npm. No cleanup. No reconnect logic. EventSource reconnects automatically. The browser handles it. You do not. WebSocket requires an onclose handler, a backoff timer, and state reconciliation after reconnect. EventSource requires nothing. It simply reconnects and resumes. The specification demands it. The Server Rust (axum): async fn events () -> Sse < impl Stream < Item = Result <
Continue reading on Dev.to Webdev
Opens in a new tab




