CONNECTION ERROR: Couldn't connect to node on WS

Hello!

I’ve been experiencing this issue for a while now and can’t decide if it’s a problem with Infura or with Web3.

I wrote a detailed description here: https://github.com/ethereum/web3.js/issues/3816

If you have any idea what’s wrong, please let me know. Thanks!

Hi @Wyctus, and welcome to the Infura community! Can you please share the code snippet you’re running to get the error? I didn’t see it in your GitHub issue.

Thanks!

There is a piece of code in the issue, but here is a little more complete example:

const provider = new Web3.providers.WebsocketProvider(process.env.INFURA_WS);
const web3 = new Web3(provider);
web3.eth.handleRevert = true;

const subscribe_to_contract_events = async contract => {    
    contract.events.SomeEventName().on("data", async event => {
        // process the event
    });
}

for (const contract of contracts) {
    try {
        await subscribe_to_contract_events(contract);
    } catch (err) {
        console.error(err);
    }
}

Where the contracts is an array of contracts, created by

new web3.eth.Contract(abi, address);

The array contains around 400 elements, so I subscribe 400 times.
Is this something to do with the many subscriptions? It’s very sporadic, but my guess was that it’s caused if many events are happening at the same time.

This problem is still present. Could anybody help?

Hi @Wyctus,

It looks like web3js has had some issues with Websocket disconnection for a while (Github pull about this same issue from a couple of years ago is here). However, the pull is very detailed and includes what steps can be taken to help mitigate the timeout/disconnection. I would suggest trying some of these steps in your code as well to see if it helps.

Thank you @Leiya_Kenney, it’s good to know that the issue is with web3js and not Infura!