Question presented to candidate: "You need to push live progress updates from the server to the browser during a long-running job — one-directional, server to client only, no client-to-server messages needed. Would you reach for WebSockets, and if not, what's the simpler alternative and how does it actually work over plain HTTP?"
What a strong answer should cover:
- For a genuinely one-directional, server-to-client-only need, Server-Sent Events (SSE) is the simpler, more appropriate choice over WebSockets — it's built on a single, long-lived, ordinary HTTP response, with no separate protocol/handshake the way WebSockets require (verified with a real WebSocket handshake in this bank's dedicated WebSockets question).
- 📌 Interview term:
text/event-stream— the realContent-Typeheader (verified directly) that tells the client (and the browser's nativeEventSourceAPI, if used) to keep the connection open and interpret the body as a real, ongoing stream of events, rather than a single, complete response. - 📌 Verified, not assumed: a real SSE server, sending real events formatted as
event: tick\ndata: {...}\n\n, was consumed by a real client — 3 real events genuinely arrived with real timestamps roughly 100ms apart (confirmed directly in the actual received data) — direct, concrete proof this is a genuine, ongoing stream of separate real events over one connection, not a single buffered response sent all at once. - A precise answer names the exact real format requirements: each event is plain text, fields separated by
\n, and a blank line (\n\n) terminates each individual event — verified directly above in the real, working format — plus the standardCache-Control: no-cacheandConnection: keep-aliveheaders real SSE responses typically set alongsideContent-Type. - The precise, honest scope: SSE is genuinely one-directional only — the client cannot send messages back over the same connection (a genuinely important, real limitation directly relevant to the prompt's stated need, which is itself one-directional) — and SSE connections are plain HTTP, so they're subject to typical HTTP proxy/timeout behavior in a way a dedicated WebSocket connection sometimes handles differently; for the prompt's exact one-directional progress-update scenario, though, SSE's simplicity (no separate protocol, works over plain HTTP, automatic reconnection built into the browser's
EventSource) makes it the more directly appropriate, simpler choice than reaching for WebSockets.
Clarifying questions expected:
- "Will genuinely bidirectional communication be needed later (the client sending real-time messages back), or is this need permanently one-directional?" — the single most decision-relevant question between SSE and WebSockets.
- "Does the deployment environment's infrastructure (a proxy, a load balancer) have any known issues with long-lived HTTP connections that SSE depends on?" — a real, practical operational concern worth confirming.
Code / implementation expected: Yes — a real SSE server and a real client genuinely receiving multiple real, time-spaced events over one connection is the concrete, convincing proof of exactly how the format and the streaming behavior work.