So what exactly is web3.js?
This is a short introduction to Web3.js, in the next articles we will dive deeper into how to use web3.js to connect smart contracts to the frontend of Dapps(Decentralized Apps).
So what exactly is web3?
Before the rise of web3, the internet has been running as web2. web2 has to do with centralization, the internet where data is stored in a centralized server. web3 has to do with decentralized apps(Dapps), which are apps that are hosted on the blockchain. web2 apps are built and are hosted on a centralized server. There is a great detail of difference between web2 and web3 but the most important thing to note is that blockchain introduced the era of web3, which is all about the decentralization of the web.
When building decentralized applications like the one on the Ethereum Blockchain, after writing your smart contracts using solidity, you would need to build a front end to connect with the smart contract so that users can be able to interact with your smart contract. This is where web3.js comes into play. web3.js is a javascript library that is used to interact with smart contracts on the etherum blockchain, it is basically used to connect your smart contract with the front end.
Before we jump into understanding how to use the web3, we need to understand that the Etherum smart contract has an API that is used to communicate with the Ethereum blockchain. In this API, there are two ways to interact with the smart contract, the first way is to read data from the smart contract, and the second allows you to modify data in a smart contract. When using this API, you will need to specify some things.
You will need to specify the smart contract, the name of the function, parameters of the function(if any) eth value(when it involves making transactions), and then any other specifications. In using the transaction API you will have to spend some ether to cover the gas fee. Another thing is that you will need to wait for an average of 15seconds for the transaction to be mined. The Ethereum API uses the json-rpc protocol which is a little bit complicated. With the json-rpc you have a single endpoint and when you want to interact with it you specify the action you want to take. The problem with this is that we have so many different smart contracts in the blockchain, and smart contracts are created all the time. So we will need different endpoints every time these smart contracts are created. This was the reason why the library web3.js was created.
Web3 is a JavaScript library and can be installed with the npm package of nodejs. In order for your web3 to work, you will need to have an Ethereum node, once you have access to Etherum node, you will need to instantiate web3. You will import the web3 and then make a connection to the Etherum node. You can use infura to create access to an Etherum node. We will be going into detail on how to do this in this web3.js series.
Other Resources
Check out details about web3 in the web.js documentation here (web3js.readthedocs.io/en/v1.3.4)
Difference between web2 and web3 (ethereum.org/en/developers/docs/web2-vs-web3)