WebSockets aren't the only real-time tool โ and often not the right one. The full menu:
Server-Sent Events โ the underrated middle
// client โ built into every browser
const es = new EventSource("/api/notifications");
es.onmessage = (e) => showNotification(JSON.parse(e.data));
// auto-RECONNECTS on drops โ WebSockets don't!
// Express server
app.get("/api/notifications", (req, res) => {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
const timer = setInterval(() => {
res.write(`data: ${JSON.stringify({ time: Date.now() })}
`);
}, 5000);
req.on("close", () => clearInterval(timer));
});The decision table
- Polling (fetch every N seconds): dead simple, fine for dashboards refreshing each minute. Start here, honestly
- SSE (server โ client only): notifications, live scores, AI-answer streaming (ChatGPT-style streaming IS SSE), build progress. Plain HTTP โ proxies and auth just work
- WebSockets (both directions): chat, multiplayer games, collaborative editing โ anything where the CLIENT also pushes frequently
The interview answer
"One-directional server push โ SSE (simpler, auto-reconnect, plain HTTP). Bidirectional โ WebSockets. Neither needed โ poll." Choosing the boring right tool over the shiny one is exactly what senior engineers listen for.