Use it for chat, live dashboards, notifications and other server-to-client push where polling is too slow. Use wss:// (TLS) in production.
WebSockets
1. Does the handshake return 101 (or 400/404/426)?
2. Does the connection drop after a fixed idle time (timeout)?
3. Which close code does the client see?
4. Does it work direct to the backend but not through the proxy?
HTTP/1.1 GET with Upgrade → every proxy forwards Upgrade/Connection → backend 101 Switching Protocols → framed messages both ways → ping/pong keepalive → close handshake
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
curl -i -N --http1.1 \Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Read-only Verification Verify boundary
# Handshake test with curl: look for "HTTP/1.1 101 Switching Protocols"
curl -i -N --http1.1 \
-H "Connection: Upgrade" -H "Upgrade: websocket" \
-H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
https://app.example.com/ws
# Same test straight to the backend, bypassing the proxy
curl -i -N --http1.1 -H "Connection: Upgrade" -H "Upgrade: websocket" \
-H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" http://10.0.0.21:8080/ws
101 direct but 400 or 200 through the proxy means the proxy does not forward the upgrade headers. In the browser, the Network panel's WS filter shows the 101, the frames and the close code.
Step 2 Common mistakes Narrow the fault
Step 3 Troubleshooting Narrow the fault
Connection closes after exactly 60 seconds of silence. A proxy idle/read timeout (nginx proxy_read_timeout, a cloud load balancer idle timeout). Raise it or send application pings more often than the shortest timeout on the path.
Handshake returns 400, 404 or 426. The upgrade headers did not reach the backend, the path is wrong, or the server requires a different Sec-WebSocket-Version/subprotocol.
Works on HTTP, fails on HTTPS. TLS terminates on a device that does not support upgrades, or the client uses ws:// from an HTTPS page (mixed content is blocked).
Close code 1006 everywhere after a deploy. Backends restarted without sending close frames; clients must reconnect with backoff.
Every proxy on the path must pass the Upgrade and Connection headers and allow long idle times. nginx needs proxy_http_version 1.1 and explicit Upgrade/Connection headers; its default 60 s read timeout closes idle sockets. Check Origin on the server to prevent cross-site WebSocket hijacking.
- Browser developer tools → Network → WS: status 101, frames, close code.
curl -i -N --http1.1 -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" https://<host>/ws— expect HTTP/1.1 101.- Proxy config: Upgrade/Connection headers and read/tunnel timeout.
ReferenceWhat it is2 sections · depth on demand
Direct Answer
A WebSocket starts as an ordinary HTTP/1.1 GET with Upgrade: websocket and Connection: Upgrade. If the server agrees it answers 101 Switching Protocols, and from then on the TCP (or TLS) connection carries WebSocket frames in both directions until either side closes it. The usual production problems are proxies: every hop must forward the upgrade headers and must not close the connection after its normal HTTP idle timeout.
Scope and canonical references
- HTTP semantics and Upgrade: HTTP, HTTP/1.1.
- Proxy configuration: Reverse Proxy, HTTP Load Balancing.
- Application-level security: HTTP Security.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Handshake
GET /ws HTTP/1.1
Host: app.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: https://app.example.com
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Accept is derived from the client's key and proves the server understood the WebSocket request. Sec-WebSocket-Protocol negotiates a subprotocol if the application uses one. ws:// is cleartext; wss:// runs over TLS and is the only sensible choice in production.
Over HTTP/2, WebSockets use the extended CONNECT method (RFC 8441) instead of Upgrade; over HTTP/3, RFC 9220. Most proxies still speak HTTP/1.1 to the backend for WebSockets.
Frames and close codes
Messages are sent as text or binary frames; ping and pong frames keep the connection alive and detect dead peers. A close frame carries a code:
| Code | Meaning |
|---|---|
| 1000 | Normal closure |
| 1001 | Going away (page closed, server restarting) |
| 1006 | Abnormal closure — the connection dropped without a close frame (never sent on the wire; reported by the client) |
| 1008 | Policy violation |
| 1009 | Message too big |
| 1011 | Server error |
Proxying
nginx does not pass hop-by-hop headers by default, so a WebSocket location needs:
location /ws/ {
proxy_pass http://app_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 1h;
}
Without a longer proxy_read_timeout (default 60 s), idle sockets are closed by nginx and the client sees 1006. HAProxy handles the upgrade automatically in HTTP mode; after the upgrade timeout tunnel applies instead of timeout client/timeout server. Cloud load balancers and CDNs have their own idle timeouts.
Scaling and state
Each WebSocket is a long-lived connection to one backend. Load balancing happens only at connect time; plan for reconnects on deploys, and share state between backends (pub/sub) rather than relying on sticky sessions alone.
Security
Browsers send cookies with the handshake, but WebSockets are not covered by CORS. The server must check the Origin header (and authenticate) or another site can open a socket with the user's cookies — cross-site WebSocket hijacking.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.