0
  1. node1= running on port 8545 => current synced block is 2000, and
  2. node2= running on port 3545 => current synced block is latest

When i am using web3 into another page and print web3 object it returns connection for node1, According to my web3.js file it should return connection for node2 because in my node1 has only 2000 block synced
I also used timeInterval() but it didn't worked


-------------------------------web3.js------------------------
var node1='http://localhost:8545'
var node2='http://localhost:3545'

    var Web3 = require('web3');
    var web3 = new Web3(new Web3.providers.HttpProvider((node1));

    if(!web3.isConnected()) {
        web3 = new Web3(new Web3.providers.HttpProvider((node2));
    } 
    else {
        web3.eth.getBlockNumber(function(er,blck){
            if(blck>7300000){
                console.log('==> ETH connection to node1);
            }else{
                web3=undefined
                web3 = new Web3(new Web3.providers.HttpProvider((node2));
                console.log('==> ETH connection to node2);
            }
        });
    }
    web3.eth.getBlockNumber(function(er,block){
      console.log(block,web3) 
      // here code works fine, web3 object is for node2
    }) 
    module.exports = web3;

    --------------------------------------------------

.

   ----------------------demo.js---------------------
    var web3 = require(./web3);
     web3.eth.getBlockNumber(function(er,block){
          console.log(block,web3) 
          // here code did not work, web3 object is for node1
     }) 
    -----------------------------------------------------
0

You have to write a watcher to actively watch for the connection if its active or not the simply watcher can be a while loop that iterates to check the isConnected state.

example

var web3 = new Web3(new Web3.providers.HttpProvider(NODE));
var interval = setInterval(() => {
  if (!web3.isConnected()) {
    web3 = new Web3(new Web3.providers.HttpProvider(NODE));
  }
}, 2000)

or can create using timeInterval

6
  • when i am using web3 object into another and try to get current block, your code didn't worked, web3.getBlockNumber() returns 22000 it should return current block , web3.eth.getBlockNumber(function(er,blck){console.log(' =>----- Block is less =>')}) – Ishwar Chandra Tiwari Mar 4 '19 at 19:32
  • This code snippet is only to give you an idea how it can be done. This is not production ready code. – Hamza Ahmed Mar 5 '19 at 3:36
  • Of course you are right but while(true) causes node app crash i also tried timeInterval but i didn't understand why web3 have object for node running on port 8545 – Ishwar Chandra Tiwari Mar 5 '19 at 5:59
  • var web3 = new Web3(new Web3.providers.HttpProvider(NODE)); var interval = setInterval(() => { if (!web3.isConnected()) { web3 = new Web3(new Web3.providers.HttpProvider(NODE)); } }, 2000) Just tested in node, Will check for the node connection every 2 seconds and if not connected then connect to the backup node – Hamza Ahmed Mar 5 '19 at 11:28
  • how could you sure the current web3 object is not old, you print web3 object into another page you will get old web3 object – Ishwar Chandra Tiwari Mar 5 '19 at 18:19

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.