What is server-sent events vs WebSockets for performance?
Answer
Server-Sent Events (SSE) and WebSockets serve different real-time communication patterns with different performance characteristics. SSE: unidirectional — server pushes to client only. Uses standard HTTP/1.1 or HTTP/2. Automatic reconnection built in. Works through proxies and firewalls. Browser sends Accept: text/event-stream; server streams data: ...\n\n text. Client: const es = new EventSource("/updates"). Performance: lightweight protocol, no extra handshake beyond HTTP. With HTTP/2, multiple SSE streams share one connection. WebSockets: bidirectional — both client and server can send. Requires protocol upgrade (ws:// or wss://). More complex proxy handling. Performance: lower overhead per message than HTTP requests, but the persistent connection consumes server resources. Choose SSE when: server pushes data (live feeds, notifications, progress), client doesn't need to send frequent messages, you want simple HTTP semantics. Choose WebSockets when: bidirectional communication (chat, multiplayer games, collaborative editing), low-latency required for client→server messages. SSE on HTTP/2 scales efficiently — each additional stream reuses existing multiplexed connections.
Previous
What is cumulative layout shift and how do you debug it?
Next
What is the scheduler API and how does it improve INP?
More Web Performance Questions
View all →- Intermediate How do you optimize React applications for performance?
- Intermediate What is the browser performance API and how do you measure performance?
- Intermediate What is layout thrashing and how do you avoid it?
- Intermediate What is resource prioritization in browsers?
- Intermediate How does SSR (Server-Side Rendering) improve performance?