Interview frequency: …
Short answer
Prefer SSE when data flows in one direction from the server to the client (such as notifications or streaming AI text), and implement it over an HTTP connection with the text/event-stream header.
Explanation
SSE is simpler than WebSockets: it uses standard HTTP, supports automatic reconnection in browsers through EventSource, and works smoothly through HTTP proxies and firewalls without a special protocol. The server keeps the connection open and sends text in the format data: {...}\n\n.
Code example
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive"
});
res.write(`data: ${JSON.stringify({ msg: "update" })}\n\n`);Common mistakes
- Forgetting to close the connection when the client leaves the page by listening for the req.on('close') event.