Skip to content

WebSocket opens many connections in during and after reconnection #3804

@alvrs

Description

@alvrs

Check existing issues

Viem Version

2.31.7

Current Behavior

When the WebSocket transport loses connection, the reconnection logic can cause it to open multiple connections. It's possible to get into a situation where neither the reconnection limit (default 5) nor the delay between new connection attempts are respected, and the client ends up creating lots of open WebSocket connections.

Expected Behavior

  • The WebSocket transport should only attempt to reconnect up to the specified number of times and space out each connection attempt by the specified delay.
  • There should only be one WebSocket connection (per server and settings) that should be shared by all clients using the WebSocket transport.

Steps To Reproduce

Steps to reproduce:

  • clone the forked repo at alvrs/websocket-repro
  • pnpm install && pnpm build at root
  • pnpm install && pnpm dev --force at /environments/vite
  • open http://localhost:5173/
  • open Network inspector
  • turn "Throttling" to "Offline"
  • wait for reconnection logic to kick in
  • observe many WebSocket connection attempts to rpc.redstonechain.com
  • turn "Throttling" back to "No throttling"
  • observe multiple open WebSocket connections
CleanShot.2025-07-16.at.15.49.31.mp4

Link to Minimal Reproducible Example

latticexyz#1

Anything else?

We also experience this situation when leaving the browser tab in the background for a while. The network throttling approach described above is how we can consistently reproduce it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions