What are WebSockets and when would you use them instead of HTTP?
WebSockets are a protocol (RFC 6455) that provides full-duplex, persistent communication over a single TCP connection. Unlike HTTP's request-response model, where the client initiates every exchange, a WebSocket connection allows both the client and server to send messages independently at any time after the connection is established.
Establishment uses an HTTP upgrade handshake: the client sends an HTTP GET with `Upgrade: websocket` and `Connection: Upgrade` headers plus a `Sec-WebSocket-Key`. The server responds with 101 Switching Protocols and a `Sec-WebSocket-Accept` header derived from the key. After this, the connection switches from HTTP framing to the WebSocket binary framing protocol on the same underlying TCP socket. This design means WebSockets work through existing HTTP infrastructure (proxies, load balancers, firewalls) on ports 80 and 443.
The wire format is frame-based: frames have an opcode (text, binary, ping, pong, close), payload length, and optional masking (client-to-server frames are always masked to prevent cache poisoning attacks on intermediaries). Messages can span multiple frames (fragmentation).
When to use WebSockets over HTTP: real-time bidirectional use cases where the server needs to push data without the client polling. Classic examples: live chat, collaborative editing (Google Docs-style), multiplayer games, live trading/financial data feeds, live sports scores, and collaborative code editors. Also appropriate when you need low latency and high frequency updates — WebSocket message overhead is much lower than HTTP headers on every message.
When not to use WebSockets: simple request-response APIs, infrequent updates where Server-Sent Events (SSE) or long polling suffice, scenarios requiring stateless horizontal scaling (WebSocket connections are stateful and sticky), or when HTTP/2 server push covers the use case.
Scaling WebSocket servers requires sticky sessions (connections must reach the same server instance) or a pub-sub broker (Redis, Kafka) that routes messages to whichever server instance holds a given connection. This adds operational complexity versus stateless HTTP services.
In Python, `websockets` or `fastapi`/`starlette` WebSocket support is common. In Node.js, `ws` or `socket.io` (which adds fallbacks and rooms). `socket.io` is not WebSockets — it has its own protocol with polling fallback and higher-level abstractions.
Correctly explains the full-duplex persistent connection model, the HTTP upgrade handshake, and gives concrete use cases where WebSockets are appropriate over HTTP polling.
All of the above plus: explains the scaling challenges (sticky sessions, pub-sub broker), distinguishes WebSockets from Server-Sent Events and socket.io, discusses frame-level protocol details, and articulates when NOT to use WebSockets.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment