Go Ethereum – How to Connect to Rinkeby Using Web3 from 8545 Node JS?

go-ethereum

I am trying to connect to a rinkeby full node in ubuntu terminal and i have a node js app deployed trying to connect using web3 when consoling the nodejs app getting connection error

  if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    } else {
        // set the provider you want from Web3.providers
        web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    }


Error: CONNECTION ERROR: Couldn't connect to node http://localhost:8545.
    at Object.InvalidConnection (/var/www/ethernode/node_modules/web3/lib/web3/errors.js:31:16)
    at HttpProvider.send (/var/www/ethernode/node_modules/web3/lib/web3/httpprovider.js:93:18)
    at RequestManager.send (/var/www/ethernode/node_modules/web3/lib/web3/requestmanager.js:58:32)
    at Eth.get [as coinbase] (/var/www/ethernode/node_modules/web3/lib/web3/property.js:107:62)
    at app.get (/var/www/ethernode/app.js:37:29)
    at Layer.handle [as handle_request] (/var/www/ethernode/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/www/ethernode/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/var/www/ethernode/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/www/ethernode/node_modules/express/lib/router/layer.js:95:5)
    at /var/www/ethernode/node_modules/express/lib/router/index.js:281:22

some one in stack overflow told to change the localhost to public Ip address , and that also didn't work , some one tell me what is happening here ?

Best Answer

Run the geth node on rinkeby testnet:

$ geth --rinkeby --ws --wsaddr 0.0.0.0 --wsport 8546 --wsorigins * --datadir /.rinkeby --cache 512 --rpc --rpcapi personal,eth,network --rpcaddr 0.0.0.0 --rpcport 8545 --fast --bootnodes enode://a24ac7c5484ef4ed0c5eb2d36620ba4e4aa13b8c84684e1b4aab0cebea2ae45cb4d375b77eab56516d34bfbd3c1a833fc51296ff084b770b94fb9028c4d25ccf@52.169.42.101:30303

make sure it's synced up:

$ geth attach ipc:/.rinkeby/geth.ipc
> eth.syncing
{
  currentBlock: 405371,
  highestBlock: 837461,
  knownStates: 272806,
  pulledStates: 269942,
  startingBlock: 271758
}

connect provider:

new Web3.providers.HttpProvider('http://localhost:8545')