4

I use websocket (ws) connection 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
  • 1
    I also ran into this problem, seems like an intermittent issue mostly. Refreshing usually fixes it. Commented May 21, 2018 at 20:33

3 Answers 3

8

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

4
  • The only stable solution of this pro. is setup your own node on server. Commented May 22, 2018 at 3:41
  • Any idea on how to set our own node server? Commented Jun 16, 2018 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
    Commented Jul 7, 2018 at 8:47
  • 1
    Yeah. There's real need for a comprehensive guide on it. Commented Jul 7, 2018 at 18:08
0

A common scenario under which this may also happen:

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

  1. You are using JavaScript / TypeScript to connect to WebSocket node provider
  2. You are using asynchronous functions
  3. You forgot to await your transaction or web3 call
  4. A Promise for a new transaction is still created and put in the event loop regardless of await
  5. But you did not wait for it, your Node command line program terminates meanwhile
  6. When Node terminates, it disconnects the websocket
  7. Now your promise gets out from the event loop, ready to start the transaction
  8. But the websocket has been already closed, so you get CONNECTION_ERROR

Solution: Make sure all of your transactions and Ethereum calls await correctly.

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.