Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to communicate with public Ethereum Blockchain on a web server?

Tags:

ethereum

geth

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)

like image 456
delete Avatar asked May 13 '19 15:05

delete


People also ask

Which library do you use to communicate with ethereum Blockchain?

Web3 is a collection of libraries that allows you to interact with an Ethereum node. These could be local or remote nodes of the contract through HTTP, IPC or Web Sockets.

Does Ethereum run on a server?

Ethereum clients can run on your computer, laptop, server, or even a single-board computer.


1 Answers

But how can I communicate with the REAL public Blockchain?

In order to connect to Ethereum Public Blockchain (an Ethereum node) with remote procedure call (RPC) on the Main Net, You need an Ethereum node. There are a few ways to do this. You can run your own Ethereum node with Geth or Parity. But this requires downloading a lot of data from the public blockchain and keeping it in sync. That's a huge thing to do.

Alternatively, you can use Infura (https://infura.io/) to access an Ethereum node (Ethereum Public Blockchain) without having to run any node by yourself. Infura provides a remote Ethereum node for free. All you need to do is sign up and obtain an API key and the RPC URL to connect.

The Infura RPC URL should look like this:

https://mainnet.infura.io/YOUR_INFURA_API_KEY  

Now you can use this RPC URL to communicate, like

const Web3 = require('web3')
const rpcURL = '' // Your RPC URL with infura key goes here,i.e. https://mainnet.infura.io/YOUR_INFURA_API_KEY 
const web3 = new Web3(rpcURL)
const address = '' // Your ethereum account address goes here
web3.eth.getBalance(address, (err, wei) => {
  balance = web3.utils.fromWei(wei, 'ether')
})

Do I need to run geth on the Web server and connect to its instance?

Already covered in the first answer, this can be another approach to communicate.

Or is there any public network available that could be used? (if we can trust it)

There are ethereum Main Net where the real token transaction occurs and Test Net that carry no real-world value. Before a project launches on the Ethereum blockchain it's best to run the whole scenario in a Test Net environment to find and fix security concerns. There are lots of test net services available. Like Ropsten, Kovan, Rinkeby etc. Just search the internet for "ethereum mainnet testnet" to learn more. Hope helps.  

like image 180
Syed Ekram Uddin Avatar answered Nov 22 '22 13:11

Syed Ekram Uddin