# Common Interview and Exam Questions — WebSockets & Server-Sent Events

Source: https://www.skillbyai.com/en/realtime-web/rt-interview

> Answer the most frequent conceptual and system-design questions about real-time web communication.

## How to structure an answer

Interviewers rarely want a definition alone; they want a **choice with reasons**. A strong answer has three parts: the mechanism, the trade-off and a concrete example. Asked "WebSocket or SSE for notifications?", say: notifications flow one way, so SSE fits; it works over plain HTTP with existing cookies, reconnects and resumes on its own; a WebSocket would add two-way capability we do not need, plus our own reconnect logic. Asked to **design a chat system**, cover: WebSocket connections to stateless gateway servers behind a load balancer; messages saved to a database first and then published through Redis pub/sub or Kafka to the gateways holding the room's members; per-room sequence numbers so clients resume after reconnecting; heartbeats; authentication at upgrade plus authorisation per room; and push notifications for offline users. Mention numbers only as rough estimates, and say what you would measure. Admitting "I would load-test this" is better than inventing a precise figure.

## Questions to practise aloud

1) How does the WebSocket handshake work, and why is `Sec-WebSocket-Key` not security? 2) How does an SSE client resume after a drop? 3) What is a half-open connection and how do you detect it? 4) How do you broadcast to a room when users are spread across servers? 5) What is CSWSH and how do you prevent it? 6) How do you handle a client that reads slower than you send?

## Answering like a doctor

A good doctor does not just name a medicine; they say what is wrong, why this medicine fits, and what side effects to watch for. Treat "SSE or WebSocket?" the same way: diagnosis (data direction and rate), prescription (the transport) and side effects (operational cost).

## Do not forget the offline user

In chat or notification designs, candidates often stop at "push over the WebSocket". Ask what happens when the user has no open connection: messages must be stored, and a mobile push notification or an unread count on next login fills the gap.

**Quiz:** In a multi-server chat design, why should a message be written to the database before it is published to Redis pub/sub?

- [ ] Redis pub/sub can only publish ids that exist in a database
- [x] So that clients who are offline or reconnecting can still fetch the message later
- [ ] Because publishing is slower than writing
- [ ] To avoid needing a WebSocket at all

*Answer:* So that clients who are offline or reconnecting can still fetch the message later. Pub/sub only reaches currently connected subscribers. Persisting first means the message survives disconnects and can be replayed or fetched.
