WebSocket
A protocol providing full-duplex communication over a single TCP connection. Starts as an HTTP upgrade request, then switches to a persistent bidirectional channel. Used for real-time apps, chat, and live data.
Full Explanation
WebSocket solves a problem HTTP wasn't designed for: server-to-client push. Instead of the client polling for updates, a WebSocket connection stays open and both sides can send messages at any time. Low overhead, low latency, no HTTP request/response cycle per message.
CDNs have a complicated relationship with WebSockets. Traditional CDN caching doesn't apply since WebSocket traffic is real-time and unique per connection. But CDNs can still proxy WebSocket connections, providing TLS termination, DDoS protection, and geographic routing. Most modern CDNs support WebSocket passthrough.
The challenge: WebSocket connections are long-lived and stateful. They tie up resources on edge servers and can't be load-balanced mid-connection. This is why some CDNs charge differently for WebSocket traffic or limit concurrent connections.
Examples
# WebSocket upgrade request
GET /ws HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
# Server response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
# Nginx: proxy WebSocket
location /ws {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400; # Keep alive for 24h
}
Video Explanation
Frequently Asked Questions
A protocol providing full-duplex communication over a single TCP connection. Starts as an HTTP upgrade request, then switches to a persistent bidirectional channel. Used for real-time apps, chat, and live data.
# WebSocket upgrade request
GET /ws HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
# Server response
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
# Nginx: proxy WebSocket
location /ws {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400; # Keep alive for 24h
}
Related CDN concepts include:
- HTTP/1.1 — The most widely deployed HTTP version. Text-based protocol with persistent connections and chunked transfer encoding. …
- HTTP/2 — Major revision of HTTP that adds multiplexing (multiple requests over one connection), header compression (HPACK), …
- Keep-Alive — Reusing a TCP connection for multiple HTTP requests instead of opening a new one each …
- TCP (TCP) — Transmission Control Protocol. The reliable, ordered, connection-oriented transport protocol underneath HTTP/1.1 and HTTP/2. TCP's three-way …