2

I use websocket (ws) coneection to Infura node

web3 = new Web3(new Web3.providers.WebsocketProvider("wss://ropsten.infura.io/ws"));

sometimes it's works fine or sometimes it give error on console :

Error: CONNECTION ERROR: Couldn't connect to node on WS.

Uncaught (in promise) Error: CONNECTION ERROR: Couldn't connect to node on WS.

I am not getting why its happening.

  • 1
    I also ran into this problem, seems like an intermittent issue mostly. Refreshing usually fixes it. – Shawn Tabrizi May 21 '18 at 20:33
4

I don't want to get a "golden shovel" reward or something but that question doesn't seem to be very old. I managed to successfully implement automatic reconnect thus I wanted to share the code:

First I created a separate function getProvider:

function getProvider() {
  const { WebsocketProvider } = Web3.providers;

  const provider = new WebsocketProvider(WEB3_WEBSOCKET_PROVIDER_URL);

  return new Promise((resolve) => {
    provider.on('connect', () => {
      logger.info('Web3 WS connected.');
      return resolve(provider);
    });

    provider.on('error', () => resolve());
  });
}

Then I have a function listen that attaches to blockchain in order to listen for events that I'm interested in:

async function listen(repositories, wallet) {
  const provider = await getProvider();

  if (!provider) {
    logger.info('Web3 WS unavailable. Reconnecting...');
    setTimeout(() => listen(repositories, wallet), 5000);
    return;
  }

  const web3 = new Web3(provider);

  provider.on('error', (e) => {
    logger.error(`Web3 WS encountered an error: ${e}.`);
    listen(repositories, wallet);
  });

  provider.on('end', () => {
    logger.error('Web3 WS disconnected. Reconnecting...');
    listen(repositories, wallet);
  });

  const contractsConnector = contractsConnectorFactory(web3);
  const contracts = await contractsConnector.connectAll();

  const listeners = listenersFactory(repositories, contracts, wallet);
  listeners.attachAll();
}

This way whenever the connection closes I make sure that I won't miss any events that were thrown in the meantime. Hope that helps anyone that will have problems with that.

2

I've also had this problem. I have been able to catch it (somewhat) using:

    const providerPath = `wss://rinkeby.infura.io/ws`
    web3 = new Web3() 
    const eventProvider = new Web3.providers.WebsocketProvider(providerPath)

    //listen for disconnects
    eventProvider.on('error', e => handleDisconnects(e));
    eventProvider.on('end', e => handleDisconnects(e))

    web3.setProvider(eventProvider)

    function handleDisconnects(e) {
      console.log("error",e);
    }

I haven't been able to successfully catch/re-connect though

  • The only stable solution of this pro. is setup your own node on server. – Chirag Maliwal May 22 '18 at 3:41
  • Any idea on how to set our own node server? – Sowmay Jain Jun 16 '18 at 11:23
  • @SowmayJain I've had the exact same experience, so want to setup my own node, but I'm not 100% sure how. I've asked the question here: ethereum.stackexchange.com/questions/50627/… – mesqueeb Jul 7 '18 at 8:47
  • 1
    Yeah. There's real need for a comprehensive guide on it. – Sowmay Jain Jul 7 '18 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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