WebSocket closed with code 1006
The connection died with no Close frame. Four causes, and how to tell them apart.
What it means
1006 never travels over the network: RFC 6455 § 7.4.1 forbids it in a Close frame. The client sets it when the connection died without a Close frame.
How to fix it
The event carries code: 1006, reason: "", wasClean: false. The spec forbids telling a script whether DNS, the port, TLS or the handshake failed, so pages cannot scan your network.
1005 is different: a Close frame arrived without a status code.
On a failed handshake Chrome's console shows Unexpected response code: 403 (variants: Error during WebSocket handshake).
1. The handshake failed. The Upgrade got 404, 403, 502 or a redirect instead of 101, and the browser will not follow the redirect. Check:
curl -i -N --http1.1 --max-time 5 \
-H "Connection: Upgrade" \
-H "Upgrade: websocket" \
-H "Sec-WebSocket-Version: 13" \
-H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" \
https://example.com/ws
Keep --http1.1: otherwise curl negotiates HTTP/2 over TLS and drops the Upgrade headers. The key is the RFC sample (a real client sends a fresh random one, RFC 6455 § 4.1); the expected answer is Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=; for other keys use the Sec-WebSocket-Accept calculator. 101 means the handshake works; any other status is the cause.
2. An idle timeout on a proxy. Often 60 seconds: nginx proxy_read_timeout 60s, AWS ALB idle_timeout 60 s. Tell: the 1006 lands on the same second every time.
location /ws {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
Add a heartbeat: other proxies in the chain have their own timeouts.
const ping = setInterval(() => {
if (socket.readyState === WebSocket.OPEN) socket.send('{"type":"ping"}');
}, 30_000);
socket.addEventListener('close', () => clearInterval(ping));
nginx (on an upgraded connection) and ALB reset their timers on traffic in either direction. Have the server reply anyway: a missing reply tells the client the connection is dead. JavaScript cannot send a Ping frame (RFC 6455 § 5.5.2), so the heartbeat is an ordinary message.
3. The server process died. Restart, OOM killer, deploy. Random time, every connection drops at once.
4. The network changed. Wi-Fi to cellular, a VPN, a sleeping laptop; usual on phones. Reconnect with backoff and jitter:
let attempt = 0;
function connect() {
const socket = new WebSocket(url);
socket.addEventListener('open', () => { attempt = 0; });
socket.addEventListener('close', (e) => {
if (e.code === 1000) return; // normal close, do not retry
const delay = Math.min(30_000, 500 * 2 ** attempt++);
setTimeout(connect, delay * (0.5 + Math.random()));
});
}
connect();
Without jitter, clients dropped by one restart return at once and knock the server over again.
See it on your own traffic
Solpuga records the Upgrade request with its headers, every frame with direction, size and time to the millisecond, and marks the socket Connection closed when it ends. Equal silence before every drop means an idle timeout; frames up to the drop point at the server or the network.
Solpuga answers the browser's handshake itself and connects to the server separately. See Capture & Inspect Traffic for how Solpuga records a stream.
Tool for this page
WebSocket close code lookupEnter a WebSocket close code to get its meaning, the spec that defines it, and what usually causes it.Related
- The stream dies after 60 seconds: proxy_read_timeoutA stream that dies on the same second is a timeout. Default values in nginx, ALB, Cloudflare, Heroku and Envoy, and why a heartbeat is safer.
- SSE and the six-connection limit: the seventh tab never loadsAn SSE stream holds an HTTP/1.1 connection for as long as the tab is open, and a browser opens at most six of them per origin. The seventh tab just waits.