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.

Bastien Labelle Bastien Labelle Full stack dev w/ 20+ years of experience Mehdi Ben Haddou Mehdi Ben Haddou - Founder of Chessigma (1M+ users) & many small projects - ex Founding Engineer @Uplane (YC F25) - ex Software Engineer @Amazon and @Booking.com rayush33 rayush33 JavaScript (React.js, React Native, Node.js) Developer with demonstrated industry experience of 4+ years, actively looking for opportunities to hone my skills as well as help small-scale business owners with solutions to technical problems Franck Plazanet Franck Plazanet I am a Strategic Engineering Leader with over 8 years of experience building high-availability enterprise systems and scaling high-performing technical teams. My focus is on bridging the gap between complex technology and business growth. Core Expertise: 🚀 Leadership: Managing and coaching teams of 15+ engineers, fostering a culture of accountability and continuous improvement. 🏗️ Architecture: Enterprise Core Systems, Multi-system Integration (ERP/API/ETL), and Core Database Structure. ☁️ Cloud & Scale: AWS Expert; architected systems handling 10B+ monthly requests and managing 100k+ SKUs. 📈 Business Impact: Aligning tech strategy with P&L goals to drive $70k+ in monthly recurring revenue. I thrive on "out-of-the-box" thinking to solve complex technical bottlenecks and am always looking for ways to use automation to improve business productivity. Caio Rodrigues Caio Rodrigues I'm a full-stack developer focused on building practical and scalable web applications. My main experience is with **React, TypeScript, and modern frontend architectures**, where I prioritize clean code, component reusability, and maintainable project structures. I have strong experience working with **dynamic forms, state management (Redux / React Hook Form), and complex data-driven interfaces**. I enjoy solving real-world problems by turning ideas into reliable software that companies can actually use in their daily operations. Beyond coding, I care about **software quality and architecture**, following best practices for componentization, code organization, and performance optimization. I'm also comfortable working across the stack when needed, integrating APIs, handling business logic, and helping transform prototypes into production-ready systems. My goal is always to deliver solutions that are **simple, efficient, and genuinely useful for the people using them.** legrab legrab I'll fill this later Taufan Taufan I’m a product-focused engineer and tech leader who builds scalable systems and turns ideas into production-ready platforms. Over the past years, I’ve worked across startups and fast-moving teams, leading backend architecture, improving system reliability, and shipping products used by thousands of users. My strength is not just writing code — but connecting product vision, technical execution, and business impact. Jacek Rozanski Jacek Rozanski Senior PHP/Symfony developer and DevOps engineer with 20+ years of professional experience, running opcode.pl (web development agency, est. 2004). Day job: I'm the sole backend developer at merketing company where I own and maintain 11 PHP/Symfony microservices on AWS (ECS Fargate, RDS, S3, CloudFront), handle the full CI/CD pipeline (Bitbucket Pipelines, Docker), and manage monitoring with Sentry and CloudWatch. These services handle high request volumes in production every month. What I bring to AI-built apps: - I audit and fix security issues (OWASP methodology), performance bottlenecks, and architectural problems in codebases generated by Cursor, Claude Code, Lovable, Bolt, and v0 - I refactor AI-generated prototypes into production-grade applications with proper error handling, testing, and clean architecture (SOLID, DDD, hexagonal architecture) - I set up the infrastructure AI tools don't touch: AWS hosting, CI/CD pipelines, automated deployments, database optimization, monitoring, and alerting - I integrate external services: payment providers, email systems, partner APIs, SSO/auth Tech stack: PHP 8.x, Symfony, React, Next.js, PostgreSQL, MySQL, Docker, AWS (ECS, RDS, S3, SQS/SNS, CloudFront), Terraform, Supabase. I also use AI tools daily (Claude Code, Cursor) in my own workflow, so I understand both the strengths and the gaps in AI-generated code. Based in Poland (CET timezone). Available for async work and calls during EU/US business hours. Rudra Bhikadiya Rudra Bhikadiya I build and fix web apps across Next.js, Node.js, and DBs. Comfortable jumping into messy code, broken APIs, and mysterious bugs. If your project works in theory but not in reality, I help close that gap. Dor Yaloz Dor Yaloz SW engineer with 6+ years of experience, I worked with React/Node/Python did projects with React+Capacitor.js for ios Supabase expert

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