4

I am using Meteor & Web3 to talk to a Geth node, and would like to have an event fire in web3 when the Geth node goes down.

Web3 has the ability to add callbacks to web3.eth.isSyncing to detect changes, but web3.eth.isConnected does not.

How can I detect changes in web3.eth.isConnected without having to use continuous polling?

3

You can try web3.net.listening

web3.net.listening
// or async
web3.net.getListening(callback(error, result){ ... })

This property is read only and says whether the node is actively listening for network connections or not.

Returns

Boolean - true if the client is actively listening for network connections, otherwise false.

  • 1
    Thanks for the input. I tried adding web3.net.getListening(callback(error, result){ console.log(error,result}) to my meteor app. It fires on initialisation, but not when I stop the Geth node. My console is flooded with net::ERR_CONNECTION_REFUSED messages though. – glider Apr 8 '16 at 13:48
  • @glider so i assume its polling. a workaround is to fire web3.reset() when we have a ERR_CONNECTION_REFUSED – niksmac Apr 8 '16 at 14:00
  • Yeah, web3/syncing.js polls for changes. How would I detect an ERR_CONNECTION_REFUSED event within Meteor? – glider Apr 8 '16 at 14:18
  • @glider did you managed to get it work? – niksmac Apr 10 '16 at 1:34
  • Not yet, thanks for your input though. Haven't found a way of detecting net::ERR_CONNECTION_REFUSED events yet, any thoughts? – glider Apr 10 '16 at 11:45
3

For full disclosure, and whoever may stumble upon this question in the future, the following code was what I was looking for...

function status() {
    try {
        Session.set('connected',web3.isConnected())
    }
    catch (e) {
        Session.set('connected',false)
    }
}
//Call the status function every second
Meteor.setInterval(status, 1000);
  • Yep - looking at the network traffic between the client & server side, Meteor is constantly polling the server side, albeit with really small requests. So perhaps this is the proper way to do it. – glider Apr 12 '16 at 13:26

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.