Is there a way to listen for web3 connection to node continuously and restart if it fails. I am expecting some similar to this. It is something very common but i can't find this in documentation. If not what approach you all use to deal with disconnection and error.

Note: I am referring to web3js v1.0

var web3 = new Web3(provider);
    .on('disconnect', function() {
        web3 = new Web3(provider);
    .on('error', function() {
        web3 = new Web(3provider);

Also should i use web3 = new Web3(provider) or web3.setProvider(provider) to reinit?


I don't think there is such event in the web3 library, but there is another method that does something similar. isListening ref

You can create a function that will keep polling web3 using isListening to check if is still connected or no (setInterval might help to achieve it) and reconnect it in case is down.

web3.setProvider is used in case you want change the current provided with another one, not sure you will have the right behaviour in case you're going to set the same provided. You need to try out. Creating a new instance with web3 = new Web3(provider) could be safer.

| improve this answer | |
  • isListening apparently is for http on infura, not websockets, which you now will need for subscribing to events in web3 1.0. community.infura.io/t/infura-stop-about-1-hour/104/23 – TrumpPaiPence Jul 17 '19 at 6:52
  • the OP never mentioned that it was for websockets or HTTP – mirg Jul 18 '19 at 14:57
  • 1
    It's virtually implied they are using a WebSocket connection if they need to worry about connection persistence in 1.0. All of the http methods requiring such are deprecated in favor of connecting via WebSocket. The only solution I have found is to setInterval a restart of the process. Everything is broken and with workarounds. 2.0 will solve this, allegedly. Your answer doesn't work, hence the downvote. Not personal. I appreciate you thinking of solutions. – TrumpPaiPence Jul 21 '19 at 14:25
  • yes you're right actually – mirg Jul 23 '19 at 13:53

Also should i use web3 = new Web3(provider) or web3.setProvider(provider) to reinit?

In general case when you have to connect to Ethereum network, it is recommended that you use the getDefaultProvider() method because it will connect to both Etherscan and INFURA simultaneously. For example.... If your use case is to connect to an existing Web3 Provider

// When using a Web3 provider, the network will be automatically detected

// e.g. HTTP provider
let currentProvider = new web3.providers.HttpProvider('http://localhost:8545');

let web3Provider = new ethers.providers.Web3Provider(currentProvider);

Is there a way to listen for web3 connection to node continuously and restart if it fails.

I don't know if there are guideline in Ehthereum for this, but I think you should implement some logic to manage this scenario, keeping in mind this:

WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time. The client establishes a WebSocket connection through a process known as the WebSocket handshake. This process starts with the client sending a regular HTTP request to the server.

You are sure the client, so you will have way to play/intercept with Events in order to manage Connection Error. Take a look at CloseEvent in the docs of WebSocket and search or try to write something with Web3.

A CloseEvent is sent to clients using WebSockets when the connection is closed. This is delivered to the listener indicated by the WebSocket object's onclose attribute.

| improve this answer | |

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.