9

I have heard that web3 is supposed to have global scope, something similar to document or window.

What is the reasoning behind this architectural choice? Is this still an underlying assumption?

In a more and more modularized Javascript landscape, I wonder if this is really the way to go.

3
  • 1
    Can you cite a reference? You can have as many instances of web3 as you like, var web3 = new Web3(). (just be careful not to manage events across instances of web3, it won't work and fails silently, and contracts cache their instance of web3.eth...). Or did you mean something else? – Paul S Apr 4 '16 at 17:27
  • I didn't do enough research on this before I asked it seems. I had heard this from some people, but I have now found a reference here: gist.github.com/frozeman/fbc7465d0b0e6c1c4c23 It seems to be an issue that is still under debate. – Karl Floersch Apr 4 '16 at 17:32
  • 1
    I just provided an answer which links to the discussion and pulls out a quote from the proposal. Apologies for not seeing that thread earlier! Though, hopefully this will get more eyes on the problem before they make a decision. – Karl Floersch Apr 4 '16 at 17:42
3

Mist browser may include web3 as a global variable at some point.

As of right now, the discussion of how to include web3 in the Mist browser is still being discussed here:

https://gist.github.com/frozeman/fbc7465d0b0e6c1c4c23

If you have an argument for or against this proposition, I would suggest you post on the gist. I'm sure they would appreciate your input!

An excerpt from the gist proposing three options to handle web3 in the Mist browser:

Mist web3 loading proposal

/*
Basically "web3" comes from Mist,
but "Web3" CAN come from  the dapp.
A Dapp has 3 ways to use web3.
2. and 3. would work when in Mist and outside.
*/

// 1. simply use, web3 comes already defined
web3

// 2. optional use web3 from mist, OR load if outside of mist
if(typeof web3 === 'undefined')
  web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

// 3. always use web3 provided by the dapp ("Web3" won't be supplied by Mist), but the provider from mist
if(typeof web3 !== 'undefined')
  web3 = new Web3(web3.currentProvider);
else
  web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
  
// Add RPC version?

@frozeman

3
  • if you only allow one instance of web3 you solve some bizarre event problems I've seen, but you also eliminate the ability to verify a transaction has been mined by more than the node you are talking to over RPC. IMHO that's not very secure. But maybe I should take that debate over to the gist... – Paul S Apr 4 '16 at 18:00
  • Yeah I agree. I think that people should contribute to that gist. It seems like a big design choice that really needs to be vetted. I just added a suggestion to go comment on the gist if you have an opinion. Also, I think you have a good point. Really curious about how all this turns out. – Karl Floersch Apr 4 '16 at 18:53
  • how does this answers the question of yours? – niksmac Apr 9 '16 at 2:55

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.