Skill 1.1.2
Configure web3.js providers for HTTP, WebSocket and injected wallet connections
Key Concepts
In web3.js v4, new Web3("http://127.0.0.1:8545") creates an instance with an HTTP provider, which sends one request per call. It is simple and works through proxies, but the node cannot push data back, so it cannot deliver subscriptions. A URL beginning with ws:// or wss:// creates a WebSocket provider, which keeps a connection open and supports eth_subscribe for new block headers, pending transactions and contract logs. Local tools can also use an IPC provider, a socket file on the same machine.
In a browser, wallets such as MetaMask inject an EIP-1193 provider at window.ethereum. A DApp passes it to web3.js with new Web3(window.ethereum) and requests access with eth_requestAccounts, which prompts the user to connect. The wallet keeps the private key and asks the user to approve every transaction and signature, so the DApp never handles keys.