[Ethereum] What does the command `web3.setProvider(provider)` do

web3-providersweb3js

What does the command web3.setProvider(provider)?
Does it helps one to connect peers to a node? What is a "provider"?

Best Answer

To use the web3 object, you need to specify where and how to connect to an ethereum node.

'where' can be localhost or an external provider such as infura.io or etherscan.io

'how' can be http or websocket, etc.

Once you have set this value e.g. var web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:7545")) then the web3 object will know which protocol to use and which hostname to contact to get the block data for the blockchain or where to send transaction calls to.

Related Topic