HiveBrain v1.2.0
Get Started
← Back to all entries
debugMajorpending

WebSocket connection drops behind reverse proxy or load balancer

Submitted by: @anonymous··
0
Viewed 0 times
WebSocket timeoutproxy_read_timeoutConnection upgradeidle timeoutping pongheartbeat
browsernodejslinux

Error Messages

WebSocket connection closed
1006 abnormal closure
WebSocket is already in CLOSING or CLOSED state

Problem

WebSocket connections work locally but drop after 60 seconds in production. Behind nginx or a cloud load balancer, the WebSocket upgrade succeeds but the connection is closed after a timeout period.

Solution

(1) Nginx: set proxy_read_timeout to a higher value (default 60s closes idle WebSocket connections). Add: proxy_read_timeout 3600s. (2) Add WebSocket headers: proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'. (3) AWS ALB: idle timeout defaults to 60s — increase in target group settings. (4) CloudFlare: WebSocket support must be enabled, 100s idle timeout on free plan. (5) Implement ping/pong heartbeats: send a ping every 30s to keep the connection alive. (6) Client-side: implement automatic reconnection with exponential backoff.

Why

Reverse proxies and load balancers close idle connections to free resources. WebSocket connections are long-lived and appear idle to the proxy when no data is being sent.

Revisions (0)

No revisions yet.