WebSockets & Server-Sent Events
WebSockets provide full-duplex, bidirectional communication over a single persistent TCP socket. Server-Sent Events (SSE) offer lightweight, unidirectional push streaming over standard HTTP.
Think of it like this: HTTP is mailing letters back and forth. Server-Sent Events is tuning into an ongoing FM radio transmission. WebSockets is an open telephone call where both parties can talk and listen simultaneously with zero delay.
Step 1 of 6
Resolve the host
Translate example.com into an IP address before any connection starts.
Syntax
GET /chat HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==Worked example
Request
GET /feed HTTP/1.1
Accept: text/event-streamResponse
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
event: update
data: {"price": 105.4}Server-Sent Events streaming real-time JSON payload updates to the browser over a persistent HTTP response stream.
The WebSocket Protocol Upgrade
Clients initiate an HTTP handshake with "Connection: Upgrade" and "Upgrade: websocket". Upon receiving "101 Switching Protocols", both sides switch to low-overhead binary/text framing.
WebSockets vs Server-Sent Events (SSE)
- Directionality — WebSockets is full-duplex (two-way); SSE is unidirectional (server-to-client only).
- Reconnection & Proxying — SSE uses standard HTTP text/event-stream with native auto-reconnection and standard proxy support; WebSockets requires custom reconnection logic.
- Ideal Workloads — Use SSE for LLM AI token streaming and news tickers; use WebSockets for real-time multiplayer gaming and collaborative editors.
Study kit
Module 16 of 18
Intermediate · 3 min · 0 of 18 complete
Module status
Not completed yet
Notessaved locally+
Notes stay in this browser. They are not uploaded.
Suggest an editLearning graph
Prerequisites
Discussion
Loading discussion…
Sign in to ask a question.