Loading...
The provider helps web3 dapps to talk or interact with the blockchain. These Providers take JSON-RPC requests and return the response. HTTP , Web Socket , IPC socket based server takes this request and returns the response. Simply, Web3 Provider is a server/website running geth or parity node which talks to Ethereum network.
An Ethereum node (like geth or parity) is a program which serves as a node for the Ethereum blockchain, and through which a user can mine Ether, create software which runs on the EVM and is part of ethereum network.
Web3's web.providers helps dapps or users to interact with those nodes engaged in Ethereum consensus. We will take a look at different web3 functions it provides.
Web3 Providers are of three types,
As name suggests http provider is a node which communicates through http protocols. Http has request response messaging pattern , thus to interact with a http provider or communicate with it request must be send everytime to gete response.
HttpProvider() takes two arguments,
We can initialise http provider as follows :
const Web3 = require('web3');
var options = {
keepAlive: true,
withCredentials: false,
timeout: 20000, // ms
headers: [
{
name: 'Access-Control-Allow-Origin',
value: '*'
},
{
...
}
],
agent: {
http: http.Agent(...),
baseUrl: ''
}
};
var provider = new Web3.providers.HttpProvider('http://localhost:8545', options);
Note that rpc url can be any http provider url , http://localhost:8545 is used as an example which implies that our provider is a local node serving at port 8545.
Websocket Provider uses web socket communication protocol over TCP that enables bidirectional communication between client and server. Unlike with HTTP protocol we do not need to do use request response messaging pattern. Once the connection is established, the server can send as many requests as it prefers.
WebsocketProvider() also takes two arguments,
We can initialise Web socket provider as follows :
const Web3 = require('web3');
const options = {
timeout: 30000, // ms
// Useful for credentialed urls, e.g: ws://username:password@localhost:8546
headers: {
authorization: 'Basic username:password'
},
clientConfig: {
// Useful if requests are large
maxReceivedFrameSize: 100000000, // bytes - default: 1MiB
maxReceivedMessageSize: 100000000, // bytes - default: 8MiB
// Useful to keep a connection alive
keepalive: true,
keepaliveInterval: 60000 // ms
},
// Enable auto reconnection
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
}
};
const ws = new Web3.providers.WebsocketProvider('ws://localhost:8546', options);
The IPC provider is running a local node. It gives the most secure connection.
var Web3 = require('web3');
var net = require('net');
var ipc_provider = new Web3.providers.IpcProvider('<Path to geth.ipc>', net);
We can change provider for its module
var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');
// or
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
// change provider
web3.setProvider('ws://localhost:8546');
// or
web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));
When using web3.js in an Ethereum compatible browser, it will set with the current native provider by that browser. Will return the given provider by the (browser) environment, otherwise null.
var Web3 = require('web3');
var web3 = new Web3(Web3.givenProvider);
This will return the current provider, otherwise null.
var Web3 = require('web3');
var web3 = new Web3(Web3.currentProvider);