What triggers can break a WebSocket connection?
Answer
WebSocket connections can be terminated by many factors: (1) Network interruption — WiFi dropout, mobile network switching (3G/4G), VPN reconnection; (2) Server restart or crash — the server process terminates, closing all connections; (3) Proxy/firewall timeout — HTTP proxies and load balancers often close idle TCP connections after 60-90 seconds; (4) Browser navigation — navigating away from the page, refreshing, or closing the tab; (5) Browser throttling — mobile browsers may close WebSocket connections when a tab is in the background for extended periods; (6) Server-side close — the server explicitly calls ws.close(); (7) Memory pressure — the browser kills the background tab and its connections; (8) TLS certificate issues for wss:// connections. Robust applications implement heartbeat (ping/pong) and reconnection logic to handle all these scenarios.
Previous
What browsers support WebSockets?
Next
How do you implement a WebSocket server in Node.js using the `ws` library?