What is the difference between WebSockets and Server-Sent Events (SSE)?
Compare WebSockets and Server-Sent Events (SSE): one-way vs full-duplex, protocols, reconnection, binary support, and when to choose each for real-time apps.
Expected Interview Answer
WebSockets provide a full-duplex connection where both client and server can send messages at any time, while Server-Sent Events (SSE) are a one-way stream where only the server pushes data to the client over a long-lived HTTP connection.
SSE runs over plain HTTP using the EventSource API, automatically reconnects, and is ideal for server-to-client updates like live feeds or notifications, but the client cannot send messages back on the same channel. WebSockets use a dedicated ws/wss protocol after an HTTP upgrade and support true two-way binary or text messaging, at the cost of more complex infrastructure and manual reconnection handling.
- SSE is simpler and runs over ordinary HTTP
- SSE auto-reconnects and supports event IDs out of the box
- WebSockets allow the client to send data too (full-duplex)
- WebSockets support binary frames, SSE is text-only
- Choose SSE for one-way feeds, WebSockets for interactive apps
AI Mentor Explanation
SSE is like the stadium scoreboard broadcasting live scores to every spectator — information flows only from the board to the crowd, and no fan can send data back to it. WebSockets are like a captain and coach on linked headsets: either side can speak the instant they wish, a genuine two-way channel rather than a one-directional broadcast to passive watchers.
Step-by-Step Explanation
Step 1
Direction of data
SSE is server-to-client only; WebSockets allow both client and server to send at any time.
Step 2
Underlying protocol
SSE runs over standard HTTP; WebSockets upgrade to the dedicated ws/wss protocol.
Step 3
Client API
SSE uses the EventSource API in browsers; WebSockets use the WebSocket API.
Step 4
Reconnection
SSE reconnects automatically with built-in event IDs; WebSockets require you to implement reconnection yourself.
Step 5
Payload type
SSE carries UTF-8 text events only; WebSockets support both text and binary frames.
What Interviewer Expects
- Clear grasp of one-way vs two-way communication
- Knowledge that SSE uses EventSource over HTTP
- Awareness of SSE's automatic reconnection and event IDs
- When SSE is preferable to WebSockets and vice versa
- Understanding binary support differences
Common Mistakes
- Saying SSE supports client-to-server messaging on the same stream
- Believing SSE needs a special protocol like WebSockets do
- Forgetting that SSE reconnects automatically
- Assuming WebSockets are always better even for simple one-way feeds
Best Answer (HR Friendly)
“Server-Sent Events let the server push updates to the browser over a simple one-way stream, great for live feeds. WebSockets open a two-way channel so the client can also send data instantly, which is better for interactive apps like chat or multiplayer games.”
Code Example
// SSE: one-way, server -> client, over HTTP
const events = new EventSource('/stream')
events.onmessage = (e) => console.log('Server says:', e.data)
// (client cannot send on this connection)
// WebSocket: two-way messaging
const ws = new WebSocket('wss://example.com/chat')
ws.onmessage = (e) => console.log('Received:', e.data)
ws.onopen = () => ws.send('Hello from client') // client can send tooFollow-up Questions
- When would you pick SSE over WebSockets?
- How does SSE handle reconnection and missed events?
- Can SSE send binary data?
- How do you send data from client to server when using SSE?
- How do WebSockets and SSE behave behind HTTP/2?
MCQ Practice
1. What is the key communication difference between SSE and WebSockets?
SSE only streams from server to client, while WebSockets support messaging in both directions.
2. Which browser API is used for Server-Sent Events?
The EventSource API is the standard browser interface for consuming an SSE stream over HTTP.
3. Which statement about SSE is true?
SSE runs over HTTP, is text-only, and provides automatic reconnection using the last event ID.
Flash Cards
Is SSE full-duplex? — No — SSE is one-way, server to client only.
Which protocol does SSE use? — Ordinary HTTP, consumed via the EventSource API.
Does SSE reconnect automatically? — Yes, with built-in retry and last-event-ID support.
Can WebSockets send binary data? — Yes — WebSockets support both text and binary frames, SSE is text-only.