Why is my WebSocket disconnecting?

Last updated: August 19, 2026

WebSocket disconnections or interruptions can happen for various reasons, and maintaining a stable connection is crucial for real-time data feeds like pending transactions or event updates. Here’s how you can handle WebSocket connection issues and ensure a more resilient setup.

Why Do WebSockets Disconnect?

WebSocket connections can be somewhat fragile, and disconnections may occur due to several factors:

  • Network instability: Temporary issues with your internet connection or the server can cause WebSocket connections to drop.

  • Idle timeouts: If a WebSocket connection remains inactive for too long, some servers or network layers may close the connection.

How to Handle WebSocket Disconnections

To deal with WebSocket disconnections and ensure your client maintains a stable connection, it’s important to implement retries. The easiest way to do this is by using the Alchemy SDK, which has retry logic built in. This ensures that your WebSocket connection is automatically re-established when it disconnects.

You can get started with the Alchemy SDK by following this guide: Alchemy SDK WebSockets Documentation.

Using the Alchemy SDK for Automatic Retries

The Alchemy SDK comes with built-in retry functionality that helps manage WebSocket disconnections. By using the SDK, you can automatically reconnect to your WebSocket and resume listening for events without needing to manually handle retries.

Key benefits of the SDK include:

  • Automatic reconnection: When a WebSocket connection is dropped, the SDK automatically attempts to reconnect.

  • Retry management: The SDK handles retry intervals, ensuring optimal reconnection times to reduce downtime.

WebSocket connections may disconnect due to network issues or idle timeouts, but by using the Alchemy SDK with built-in retry logic, you can ensure a more resilient connection. This will keep your application listening for real-time events even after temporary disconnections.

For more details, check out the Alchemy SDK WebSockets Documentation.

Using Other Clients (viem, ethers, web3.js)

If you're not using the Alchemy SDK, you'll need to configure reconnection behavior in your client library directly. For example, with viem, you can pass keepalive and reconnect options to the WebSocket transport:

webSocket(wsUrl, {
  keepAlive: { interval: 20_000 },
  reconnect: { attempts: 10, delay: 2_000 },
  retryCount: 5,
  timeout: 60_000,
})

Check your client library's documentation for its equivalent keepalive and reconnect settings.

Building a Resilient Long-Running Consumer

If your application needs to stay connected indefinitely, such as a bot listening for contract events across multiple chains, treat a closed WebSocket as a recoverable event rather than something that should stay open indefinitely. This holds even if only one of your chains is visibly dropping, since a silent reconnect on the others can still mean missed events. A few practices help:

  • Track the last successfully processed block for each chain you're subscribed to.

  • On reconnect, recreate your subscriptions and backfill any logs you may have missed from that last block over HTTP.

  • Dedupe events by transaction hash and log index, since backfilling can return events you already processed before the drop.

  • Log the WebSocket close code and reason, along with reconnect attempts, so you can tell whether drops are client-side, network-side, or server-side.