1

I'm not sure I fully understand how web3 interacts with the ethereum blockchain. When testing things out, I just use

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

And that allows me to access the test network locally. If I were to deploy this code on a website, what would I set this variable to, and how does that work? And what if that user is not running a local node and interacts with the blockchain through Metamask?

2

If I were to deploy this code on a website, what would I set this variable to, and how does that work?

You can replace it with your own working node or you can use some service like infura.io which allows you to "access Ethereum via the Infura load-balanced nodes and smart architecture the same way you would via your own nodes."

And what if that user is not running a local node and interacts with the blockchain through Metamask?

From MetaMask F.A.Q.:

  • MetaMask injects web3 object and convenience Web3.js library into the javascript context.

So in most scenarios you just can use this piece of code:

// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
    // Use Mist/MetaMask's provider
    web3js = new Web3(web3.currentProvider);
} else {
    web3js = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/<API_TOKEN>"));
}
  • I see, and the way metamask works for a user not running a node is by running it's own node and allowing users to interact with the blockchain via http requests, correct? – user33938 Mar 5 '18 at 19:57
  • @user33938 related ethereum.stackexchange.com/q/13362/9938. So, not particularly their own node, but your understanding is correct. – Roman Frolov Mar 5 '18 at 20:09
  • I think <API_TOKEN> is no longer needed fo infura – Badr Bellaj Aug 30 '18 at 11:03

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.