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?

2 Answers 2

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.

8
  • 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.
    – billett
    Apr 8, 2016 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, 2016 at 14:00
  • Yeah, web3/syncing.js polls for changes. How would I detect an ERR_CONNECTION_REFUSED event within Meteor?
    – billett
    Apr 8, 2016 at 14:18
  • @glider did you managed to get it work?
    – niksmac
    Apr 10, 2016 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?
    – billett
    Apr 10, 2016 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);
1
  • 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.
    – billett
    Apr 12, 2016 at 13:26

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .