2

I've looked through the documentation and haven't found that to be helpful.

I would like to know the best way to do a healthcheck on my websocket connection to geth (Yesterday, I found out that I did not receive the new event from a few days ago without any reason).

I have a solution but it is not likely the best practice approach.

const web3 = new Web3(new Web3.providers.WebsocketProvider(networkUri));

setInterval(() => {
  console.log('Check WS status');
  let subscription;
  new Promise((resolve, reject) => {
    subscription = web3.eth.subscribe('pendingTransactions')
      .on('data', function () {
        return resolve(true);
      })
      .on('error', function () {
        return reject(false);
      });
  })
    .then(() => {
      console.log('OK');
    })
    .catch(() => {
      console.log('NOK');
      reconnect();
    })
    .finally(() => {
      // unsubscribes the subscription
      subscription.unsubscribe(function (error, success) {
        if (success)
          console.log('Successfully unsubscribed!');
      });
    })
}, 30000);

Regards

0

Looks like there is an open issue on that problem:

https://github.com/ethereum/web3.js/issues/1391

https://github.com/ethereum/web3.js/issues/1558

As it states (have not tested myself) there is end event which should detect closing

const wes3 = new Web3(new Web3.providers.WebsocketProvider('wss://rinkeby.infura.io/ws'));

wes3._provider.on('end', (eventObj) => {
    console.log('Disconnected: ');
    console.log(eventObj);
});
  • Sorry for the late answer, unfortunately I never receive the 'end' event... I tried to turn off my internet connection and I did not receive anything – oktapodia May 29 '18 at 22:45

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.