Background work, webhooks and real-time · mid
Real-time: WebSockets, SSE and Socket.IO
Use Server-Sent Events for one-way server-to-client streams over plain HTTP (with automatic reconnect), WebSockets for two-way low-latency messaging, and remember that both keep connections open, so scaling needs a shared pub/sub layer between server instances.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Polling: simple, wasteful. Long polling: the server holds the request until there is data. SSE: one long HTTP response with text/event-stream; the browser's EventSource reconnects automatically and sends Last-Event-ID to resume. WebSocket: a full-duplex connection after an HTTP upgrade.
- SSE fits notifications, live scores, order status and AI token streaming; it works through most proxies and with HTTP/2 multiplexing. Server to client only.
- WebSockets fit chat, collaboration, games and trading screens; you handle heartbeats (ping/pong), reconnect with backoff, and message ordering yourself.
- Socket.IO (4.8): rooms, acknowledgements, automatic reconnection and an HTTP long-polling fallback on top of WebSockets. It has its own protocol, so a plain WebSocket client cannot talk to it.
- Scaling: each client is attached to one instance. To broadcast across instances use Redis pub/sub (the Socket.IO Redis adapter); with long polling enabled you also need sticky sessions at the load balancer.
- Auth: authenticate during the handshake (cookie or token), re-check permissions per room/topic, and plan for token expiry on long-lived connections.
- Node 22+ has a built-in WebSocket client; servers use ws or Socket.IO.
The red pen
Common traps: mistake → fix
Mistake: WebSockets for everything
Fix: SSE is simpler for one-way updates and works with normal HTTP tooling.
Mistake: One server instance keeps all connections, so broadcasting is easy
Fix: with several instances you need pub/sub between them.
Mistake: Socket.IO is just WebSockets
Fix: it is a protocol on top; clients must use the Socket.IO client.
Easy to hard
Questions interviewers ask, easy to hard
- Polling vs long polling vs SSE vs WebSockets: when do you use each?
- How does SSE resume after a disconnect?
- How do you scale WebSockets to 1 million connections across many servers?
- How do you authenticate a WebSocket connection?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.