Web3.js and Ethereum Wallet DApp – How to Connect with Public Ethereum Blockchain on a Backend/Web Server

ethereum-wallet-dappethereumjsweb3-providersweb3.phpweb3js

In the backend of a Web application I have to communicate with the public Ethereum Blockchain.

On local development machine, I run ganache as testrpc and connect with such a line of code:

web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

I've also found out that I can develop against Rinkeby testnet with:

web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/$thisistheapikey"));

But how can I communicate with the REAL public Blockchain? Do I need to run geth on the Web server and connect to its instance? Or is there any public network available that could be used (if we can trust it)?

Best Answer

Well, Rinkeby is a public testnet, but the chain that holds real Eth that is tethered to real fiat is called Mainnet.

You can connect to it through Infura:

web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/$thisistheapikey"));

On backend you can use an implementation of Web3 library for your backend language of choice. You can also run your own Mainnet node, but it won't change your code apart from the Web3 connection string, e.g.

web3 = new Web3(new Web3.providers.HttpProvider("http://my-mainnet-node.dev.local:8545"));

Pros of having your own geth node is having control over your own environment and it's free of charge under any load, unlike Infura.
Cons of this method include having to manage your own node, expenses on a good server to host your node (you will need a good SSD with large capacity, significant amount of RAM for fast synchronization and a decent processor to process txs and hold under a significant load).

Hope it helps.

Related Topic