JSON-RPC is simply a remote procedure call protocol that is used on Ethereum to define different data structures. It also defines the rules on how data structures are processed in the network. Because it is transport-agnostic, you can use it to interact with an ETH node over sockets or HTTP. If you are already well-versed on this topic and are already using your JSON and JSON-RPC knowledge to hook up to APIs or create your specifications to interact with Ethereum nodes and build on the Ethereum blockchain, you should look up Moralis for the exciting possibility of speeding up the process, skipping the clunky initiation phases and building serverless dApps seamlessly.
Moralis allows you to easily access nodes and full archive nodes with WebSocket or JSON-RPC access. Additionally, Moralis’ revolutionary middleware lets you redefine your dApp and Web3 development – by taking care of the backend processes while you handle the frontend. The process is comparatively easy and will get you shipping dApps in a matter of minutes! Moralis can even help you set up your blockchain node in record time. Get a fast intro on Moralis and dApp development now by clicking on Moralis’s quick start tutorial.
What is JSON-RPC?
In short, JSON represents a lightweight format for data interchange. Take note that it is not a programming language in itself. Rather, it is a format for structuring data so that it’s easy to transfer. We have already covered JSON in a previous blog post, so it helps to backread our previous article on JSON to get a refresher while we cover “what is JSON-RPC”.
Most popular programming languages today already have libraries that can read and write JSON structures. Using JSON makes data interchange faster, highly flexible and it helps everything run smoother.
JSON-RPC, on the other hand, refers to the remote procedure call, or RPC, that is encoded in JSON. JSON is similar in purpose to XML and the XML-RPC protocol has similarities to JSON-RPC.
Part of understanding “what is JSON-RPC” is understanding RPC itself. The concept of a remote procedure call, or RPC, in distributed computing refers to the process by which a computer program causes the execution of a subroutine or a procedure in a different location, known as an address space. Such remote address spaces in distributed computing refer to other computers in a network.
RPCs are coded as though they were just local procedure calls. In other words, a programmer will code the same thing even if the subroutine was meant to be local or remote. There is no distinguishable difference in coding for both local and remote interactions, and the programmer does not need to explicitly indicate that such a procedure call is to be run locally or remotely. So, how is this done?
What are RPCs?
RPCs, being a form of IPC or inter-process communication, utilize mechanisms that operating systems provide so that the processes themselves can manage the shared data in networks. Different processes occupy different address spaces. The address spaces can be categorized in two ways: physical or virtual. When in the same machine, or occupying the same physical space as the host machine, such processes have their own unique virtual address spaces. While the physical address is the same, they are virtually represented by different addresses. It can also be that the processes occupy different physical spaces if they are in separate host locations.
Either way, there is a request-response, or request-reply process, that occurs between these addresses, which are designated as either “clients” or “servers”. Part of understanding “what is JSON-RPC” is to grasp the dynamic between these two. The initiator of a remote procedure call is generally known as a client. This client sends a request message to a remote server so that it can execute a procedure with specific parameters in mind. A response is sent back to the client from the remote server. The process continues as a back-and-forth interaction.
The client-server interaction is most commonly employed in distributed computing and is one of the foundations of “what is JSON-RPC”. An IPC mechanism can be synchronous or asynchronous. Such synchronous and asynchronous processes form the basis of JSON-RPC and its applications as Ethereum JSON-RPC when working with Solidity, smart contracts, and the Ethereum Virtual Machine (EVM). The client-server interaction – where clients are the callers and the servers are the executors – in RPCs is implemented via the above-mentioned request-response system, that passes on messages.
Local Calls vs RPCs
Remote procedure calls or RPCs have an important and necessary distinction from local procedure calls in that they have a higher probability of failure. Network issues can cause such calls to fail. This is one of the hurdles that must be kept in mind when distinguishing between remote procedural calls versus those that are local. Thus, care is employed when writing the code for such procedures, limiting them to relatively low-level subsystems. Remote calls are also much slower compared to local calls. Another thing to note is that RPCs require some level of location transparency in their model.
Why Use RPCs?
It helps when you split applications into different parts that can act independently of each other. With what is called multi-tier architecture, you can separate presentation from logic. In the case of web apps, the front-end presentation side is split from the actual application. Through this method, one can simplify the use of alternative front-ends. It also improves security. By separating the front-end from the data itself and only limiting its access through call functions, one can reduce complexity and enhance security in the overall design.
Advantages of JSON-RPC
There are certain reasons as to why many prefer to work with JSON-RPC. First of all, it is a very simple, compact, and lightweight RPC. Apart from that, it is independent of transport and can be used with TCP/IP, HTTP, HTTPS, Unix domain sockets, avian carriers, et cetera. It can also support Null or None directly. Furthermore, JSON-RPC allows for notifications. In addition, it also has a built-in request-response matching capability through the “id” field. Moreover, there is also support for keyword parameters. These obvious advantages give insight into why it is widely used, including in blockchain and Web3 applications.
Developing on Ethereum and Web3 is one of the main reasons why someone today would like to learn more about JSON-RPC. Ethereum JSON-RPC and Web3 JSON-RPC are exciting topics but not without their challenges.
When trying to navigate your way around Ethereum JSON-RPC and using Web3 JSON-RPC, you will see beginner questions like “What is JSON-RPC?” and “What are blockchain nodes, smart contracts, Web3 JSON-RPC requests, methods, and calls?”
But by now you already have an idea of what JSON-RPC can do from the previous topic “What are RPCs?” you can see where this fits into the entire smart contract-EVM interaction.
When learning Web3 JSON-RPC and Ethereum JSON-RPC to access the Ethereum blockchain and execute smart contracts, understand that the setup of a blockchain node is critical to starting your journey.
Interacting with ETH Nodes
Software applications need to connect to ETH nodes so they can interact with the Ethereum blockchain. Interaction in this case is defined as the reading of blockchain data or the process of sending transactions to the Ethereum network.
Among the basic things that you need to remember is that JSON-RPC supports: 1) Requests (functions); and 2) Notifications (Procedures).
The answer to the question “What is JSON-RPC?” is that it’s a a stateless, lightweight RPC, that can transport data agnostically over WebSockets, HTTP, and different environments used for message-passing and is therefore crucial for developers to interact with nodes.
To proceed, you also need to be clear on two definitions: 1) An Ethereum client, according to the Ethereum website, is “an implementation of Ethereum that verifies all transactions in each block, keeping the network secure and the data accurate.” 2) A node, in addition, is a “running piece of client software”.
Easily Connect to Blockchain Nodes with Moralis
It takes a while to manually run an Ethereum node, and the process has its challenges and difficulties. You will likely run into some mistakes trying to set it up. What’s more, it could take days to weeks to sync and figure things out. It’s a good thing there are services out there that act as blockchain node providers so you don’t have to. One alternative is Moralis, which has a long list of capable features, plus cross-chain support. Today, Moralis provides cross-chain compatibility across platforms like Ethereum, Polygon, Binance (BSC), and Arbitrum. If you want a fast, full node setup with support across multiple chains, Moralis is the perfect way to do this.
With Moralis’s unique Speedy Nodes, you can get direct WebSocket and Web3 JSON-RPC access to the platform’s full nodes and full archive nodes. If you want to learn more about this quick, hassle-free node setup, view this explainer:
Once you’ve signed up to use Moralis Speedy Nodes, you are ready to proceed with the next setup of Moralis Speedy Nodes. This is quicker and easier than any other Web3 JSON-RPC and node infrastructure alternative. As such, it can save you a lot of time and cost.
Ethereum, at its core, is the state of interaction among a network of users. Smart contracts form the heart of such interactions. They allow developers to code what they want to be done. Moreover, they also execute such agreements or transactions on the blockchain. Such agreements cannot happen without JSON-RPC.
This provides some alternatives to spinning up your own node and skipping manually building a backend. Moralis provides a massively capable shortcut to dApp deployment, as you can see in the image below. It is now also time for you to understand more about how JSON-RPC works under the hood and is useful to Ethereum.
Web3 JSON-RPC is how most blockchain users interact with clients. Simply put, you can use Web3 JSON-RPC on Ethereum to define its various data structures. It also dictates the rules on how to process such data structures in the network. You can use it to interact with an ETH node over various environments, sockets, or HTTP.
Let us illustrate further how it works with smart contracts. A client uses the Ethereum JSON-RPC API to query data from a smart contract. Furthermore, it then sends this data to a node in the network. The client may use a WebSockets or HTTP endpoint from an ETH node, so that it can connect and send the API calls. You can obtain the node endpoints by using node details in various Ethereum or blockchain sites.
All Ethereum clients have a common need to implement a JSON-RPC specification to enable such interaction. Currently, this is JSON-RPC 2.0, and you can view the API on Ethereum’s website. This specification guarantees that applications use a uniform set of methods. The specification dictates the way that the blockchain and network in Ethereum functions. For more details on the parameters of JSON-RPC 2.0, you can refer to the main JSON-RPC website.
When reviewing this, you’ll notice that JSON-RPC 2.0 uses client-server architecture instead of 1.0’s peer-to-peer one where each peer was both a client and a server. Other key features are transport independence, reduced fields, an added “jsonrpc” field, added error definitions, extensions, and so forth. When further evaluating the features of 2.0 relevant to Ethereum, and the list of supported methods, you may refer to the Ethereum Wiki. The 2.0’s default endpoints include C++, Go, Py, Parity, and Hyperledger Besu.
dApp Development Made Simple
This should make you are familiar with ETH node setup and the prerequisites for interacting with Ethereum. As such, you can see the value of using Web3 JSON-RPC to make it all work.
In dApp development, you can study the JSON-RPC API on the Ethereum site to interact with ETH clients. An Ethereum node is the key to developing and using dApps that interact with the main blockchain. Composing the backend of dApps are smart contracts that run across all nodes. If you like the Ethereum network, you can easily develop a dApp that adds to the ecosystem’s growing functionality.
The frontend of dApps use the listed methods in the Ethereum JSON-RPC API. Specificially, this is how they interact with the network through your ETH node. With this, a dApp’s frontend can make API calls over HTTP or WebSockets.
Moralis Makes dApp Development Easy
By now you have a conceptual answer to “What is JSON-RPC?” and can map out its role in working with Ethereum and other blockchains. It is also crucial to building dApps to allow them to easily work with the biggest blockchains, such as Ethereum, BSC, Polygon, or others.
Building dApps can seem confusing at first, but the capable yet intuitive Web3 backend infrastructure services offered by Moralis make it all hassle-free. Moralis only requires a few lines of code in order to create a dApp or decentralized app. Explore how Moralis allows you to set up a full node and provides a complete backend. Additionally, read up on how you can accelerate dApp creation with Moralis here.