Question presented to candidate: "You're building a live chat feature — both the server AND clients need to send messages to each other at any time, not just in response to a request. Why can't a series of regular HTTP requests handle this well, and what does a WebSocket connection actually provide instead?"
What a strong answer should cover:
- Regular HTTP is fundamentally request-response: the client always initiates, the server always replies — it cannot handle the prompt's exact need (the server sending a message unprompted, at any time) without an awkward workaround like polling, which adds real latency and wasted requests.
- A WebSocket connection begins as a real HTTP request but upgrades to a persistent, genuinely bidirectional connection — after the upgrade, either side can send a message to the other at any time, with no new request needing to be initiated first.
- 📌 Verified, not assumed — the exact answer to the prompt: a real
ws-based server genuinely pushed an unsolicited welcome message to the client the instant the connection opened — the client never requested it, never sent anything first — direct, concrete proof of genuine server-initiated push, impossible with plain request-response HTTP. A real client-sent message ("hello server") then genuinely triggered a real server response echoed back — real, bidirectional, two-way communication over the same connection. - 📌 Interview term: the protocol upgrade — a WebSocket connection starts as a real HTTP request with an
Upgrade: websocketheader; on success, the same underlying TCP connection is repurposed for the WebSocket protocol — it's not a separate connection alongside the original HTTP one, but a genuine transformation of it. - A precise answer names the direct comparison to the SSE alternative covered in this bank's own dedicated question: SSE is genuinely simpler but one-directional only — for the prompt's exact chat scenario, where BOTH sides genuinely need to send messages at any time, WebSockets' real bidirectionality (verified directly above) is the actual requirement SSE cannot satisfy, making this the correct real choice for this specific prompt, as opposed to the SSE question's own genuinely one-directional scenario.
Clarifying questions expected:
- "Does the chat feature need to scale across multiple server instances, requiring messages to be relayed between instances (a real pub/sub layer, like Redis) rather than just within one process's in-memory connections?" — a real, important scaling consideration beyond a single-process demo.
- "What should happen to a message sent while the recipient is genuinely disconnected — queued for delivery, or simply lost?" — a real, concrete design decision WebSockets alone don't answer.
Code / implementation expected: Yes — a real WebSocket server genuinely pushing an unsolicited message the instant a connection opens, plus a real client message and a real server echo, is the concrete, convincing proof of exactly what genuine bidirectionality provides over plain request-response HTTP.