New PublicClient instances created with WSS transport immediately fail to watch contract events if created after a period of the app inactivity - i.e. if no PublicClient was created or used for a few hours. The issue isn't tied to the lifetime of any single instance but seems related to the time elapsed since the last active use across all instances. I tried creating multiple instances to avoid WSS closing due to inactivity.

Here's how I'm setting up the PublicClient:

  chain: arbitrum,
  transport: webSocket(wssEndpoint, {
    key: 'arbitrum',
    name: arbitrum.name,

When attempting to use these after a period of inactivity, I receive errors indicating that the WebSocket connection is closed when calling publicClient.watchContractEvent:

WebSocket request failed.
URL: wss://host.com/provider_secret_key
Request body: {"jsonrpc":"2.0","id":94,"method":"eth_getLogs","params":[...],"fromBlock":"0xb8ccdf8"}]}
Details: Socket is closed.
Version: [email protected]

On the other side Chainstack reports HTTP 499 errors.

This seems not to be related to the number of instances but rather to the duration of inactivity.

Are WSS sockets reused by createPublicClient? If so, is there a method to either ensure a fresh connection for each new instance or to keep the existing connections alive?

Is there a way to explicitly close or destroy a PublicClient instance to manage these connections better?


1 Answer 1


You already have a comment to this question here: https://github.com/wevm/viem/discussions/2011

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.