Claude Code realtime

WebSocket Drops Connection with No Automatic Reconnection

Your application's WebSocket connection drops after network interruptions, server restarts, or idle timeouts, and never attempts to reconnect. Users see stale data, missing notifications, and a broken real-time experience until they manually refresh the page.

WebSocket connections are inherently fragile. Mobile networks switch between WiFi and cellular, laptops sleep and wake, load balancers have idle timeouts, and servers restart during deployments. Without automatic reconnection, every one of these common events breaks your app for affected users.

The issue is worst on mobile devices where network conditions change frequently. Users may not realize their connection is dead until they wonder why they haven't received any updates in minutes.

Error Messages You Might See

WebSocket connection closed unexpectedly WebSocket is already in CLOSING or CLOSED state Error: Connection timeout failed: Connection reset by peer WebSocket handshake: 502 Bad Gateway
WebSocket connection closed unexpectedlyWebSocket is already in CLOSING or CLOSED stateError: Connection timeoutfailed: Connection reset by peerWebSocket handshake: 502 Bad Gateway

Common Causes

  • No reconnection logic — The code creates a WebSocket connection but has no 'onclose' or 'onerror' handler to trigger reconnection
  • Single connection attempt — The connection is established once on page load and never retried
  • Missing heartbeat/ping — No keepalive mechanism to detect dead connections before the OS TCP timeout (which can be minutes)
  • Server-side idle timeout — Load balancers or proxies (Nginx, Cloudflare) close idle WebSocket connections after 60-120 seconds
  • No state recovery after reconnect — Even when reconnection works, missed messages during the disconnection are never recovered

How to Fix It

  1. Implement exponential backoff reconnection — On disconnect, retry at 1s, 2s, 4s, 8s, 16s intervals up to a maximum of 30s. Add jitter to prevent thundering herd
  2. Add client-side heartbeat — Send a ping message every 30 seconds and expect a pong within 5 seconds. If no pong, treat the connection as dead and reconnect
  3. Use a WebSocket library with built-in reconnection — Libraries like reconnecting-websocket, Socket.IO, or Ably handle reconnection automatically
  4. Implement message recovery — Track the last received message ID. On reconnect, request missed messages from the server since that ID
  5. Show connection status to users — Display a banner when disconnected so users know data may be stale

Real developers can help you.

Yovel Cohen Yovel Cohen I got a lot of experience in building Long-horizon AI Agents in production, Backend apps that scale to millions of users and frontend knowledge as well. Simon A. Simon A. I'm a backend developer building APIs, emulators, and interactive game systems. Professionally, I've developed Java/Spring reporting solutions, managed relational and NoSQL databases, and implemented CI/CD workflows. ISHANTDEEP SINGH ISHANTDEEP SINGH Senior Software Engineer with 7+ years of experience in React, JavaScript, TypeScript, Next.js, and Node.js. I’ve also worked as a tech lead for startups, owning end-to-end technical execution including architecture, development, scaling, and delivery. I bring a strong mix of hands-on coding, product thinking, and technical leadership, and I’m comfortable building products from scratch as well as improving and scaling existing systems. Bastien Labelle Bastien Labelle Full stack dev w/ 20+ years of experience Kingsley Omage Kingsley Omage Fullstack software engineer passionate about AI Agents, blockchain, LLMs. Nam Tran Nam Tran 10 years as fullstack developer Matthew Jordan Matthew Jordan I've been working at a large software company named Kainos for 2 years, and mainly specialise in Platform Engineering. I regularly enjoy working on software products outside of work, and I'm a huge fan of game development using Unity. I personally enjoy Python & C# in my spare time, but I also specialise in multiple different platform-related technologies from my day job. Pratik Pratik SWE with 15+ years of experience building and maintaining web apps and extensive BE infrastructure Meïr Ankri Meïr Ankri Full-stack developer specializing in React / Next.js / Node.js with 6+ years of experience. I've worked across various sectors including automotive (Reezocar/Société Générale), healthcare (Medical Link SaaS), and e-commerce (Glasman). I build web apps end-to-end, from architecture to production, with a focus on scalability, performance, and code quality. I also mentor junior developers and contribute to technical decisions and code reviews. PawelPloszaj PawelPloszaj I'm fronted developer with 10+ years of experience with big projects. I have small backend background too

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help

Frequently Asked Questions

Why does my WebSocket disconnect after 60 seconds of inactivity?

Most load balancers and reverse proxies (Nginx, Cloudflare, AWS ALB) have a default idle timeout of 60-120 seconds. Send periodic ping messages (every 30s) to keep the connection alive, or increase the proxy timeout.

Should I use WebSocket or Server-Sent Events?

Use WebSocket for bidirectional communication (chat, collaborative editing). Use SSE for server-to-client only (notifications, live updates). SSE has built-in reconnection and is simpler to implement.

Related Claude Code Issues

Can't fix it yourself?
Real developers can help.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help