Lesson 10 of 30
HTTP is a request, then a response, then the connection's job is done — WebSockets keep one connection open indefinitely so either side can send a message at any moment, which is what real-time features actually need.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
Why doesn't a plain fetch()-based request/response pattern fit a feature like live chat messages arriving from other users?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A normal fetch follows a strict pattern: the client asks, the server answers, done. For a live chat message, a stock price ticking, or a collaborative cursor moving, that pattern doesn't fit — the server needs to push data to the client the instant something happens, with no request to answer.
A WebSocket starts as a normal HTTP request, then upgrades to a persistent, full-duplex connection — both sides can send messages independently, at any time, over the same open connection, until either side closes it.
const socket = new WebSocket("wss://api.example.com/chat");
socket.onopen = () => socket.send(JSON.stringify({ type: "join", room: "general" }));
socket.onmessage = (event) => {
const msg = JSON.parse(event.data);
console.log("received:", msg);
};
socket.onclose = () => console.log("disconnected");
HTTP: client asks, server answers, connection closes. To get updates, the client must keep asking (polling).
onclose, wait (often with exponential backoff), and re-establish the connection and any necessary re-subscription."How would you build a live chat feature?" is a great practical systems question, and correctly reaching for WebSockets (over polling, and knowing why SSE wouldn't fit a bidirectional chat) shows you understand the actual constraint — need vs. overkill — not just "there's an API for that."