Skip to main content
Background Image
  1. Posts/

JSON RPC and JRPC: Revolutionizing Your Remote Procedure Calls

·888 words·5 mins
Table of Contents

What is an RPC?
#

RPC, short for Remote Procedure Call, is a protocol that allows one piece of software to request a service exposed by another piece of software.
These two pieces of software can reside on the same machine or on different machines, even across different networks.

In essence, an RPC call consists of invoking a function or procedure hosted on a server, a separate device, or inside a container.
When an RPC call is made, the underlying system handles the entire communication process between the two devices, making the remote procedure virtually indistinguishable from a local one from the developer’s perspective.

RPC is fundamental in many situations, such as:

  • Distributed data storage systems
  • Web services
  • Distributed computing projects
  • Operating systems
  • Microservices

The Value of RPC
#

Adopting RPC offers numerous benefits, making it a strong choice in various contexts.
First of all, it abstracts away the complexity of networking: RPC hides the details of the network system, allowing developers to focus on application logic.
Additionally, RPC promotes a modular approach to software development, where each service can be developed, tested, and deployed independently.
Finally, thanks to its nature, RPC facilitates interoperability between different technologies and programming languages, simplifying integration between various systems or components.

JSON RPC: An Innovation in RPC
#

JSON RPC takes the concept of RPC to a new level, introducing flexibility and interactivity.
This data transmission protocol uses the JSON (JavaScript Object Notation) format to encode RPC messages, making them lightweight, easy to read and write, and independent of the programming language being used.

The protocol follows precise rules: every RPC call is encoded as a JSON object that must include the following properties:

  • method: specifies the name of the method to invoke
  • params: an array or object representing the method parameters
  • id: a unique identifier for the call
  • jsonrpc: specifies the version of the JSON RPC protocol

The response, also encoded in JSON, returns either the result of the invoked method or error details in case of failure. It includes the following properties:

  • jsonrpc: specifies the version of the JSON RPC protocol
  • id: a unique identifier for the call
  • result: contains the result of the call, which may be an object. If an error occurs, this property will not be present
  • error: returned in case of an error, with the following structure:
    • code: a number indicating the type of error. You can check the list of possible error codes and their meaning at this link
    • message: a short message describing the error
    • data: an object providing additional details about the error, optional

For more insights and further details about the protocol, I encourage you to consult the official JSON RPC documentation.

Detailed Analysis of JSON RPC
#

The JSON RPC protocol stands out from other RPC solutions due to its flexibility and interoperability.
It is not tied to a specific programming language or transport protocol.
In fact, it can be used with any language and transported over any protocol, offering unprecedented flexibility.
The protocol’s efficiency is further enhanced by its support for batch calls or notification calls, optimizing resource usage.

JRPC: Simplifying the Use of JSON RPC
#

JRPC is a project I am developing with the goal of making the implementation of the JSON RPC protocol more straightforward and intuitive.
This project consists of two key components: a server library and a client library, created to simplify the integration of the JSON RPC protocol into software applications.
The server library efficiently manages RPC requests, while the client library facilitates the processing and handling of RPC calls.
To further accelerate client-side implementation, I am also developing a CLI.

Here’s a clear and concise example of how it works:

For the server, based on Fastify:

...
const jrpcServer = new Server({
    name: 'jrpc-server',
    version: '1.0.0',
    description: 'demo server',
});

jrpcServer.addMethod({
    "name": "hello",
    "description": "A simple hello world method",
    "params": [
        {
            "name": "name",
            "description": "The name of the person to say hello to",
            "schema": {
                "type": [
                    "string",
                    "null"
                ]
            }
        }
    ],
    "result": {
        "name": "result",
        "description": "The result of the hello world method",
        "schema": {
            "type": "string"
        }
    }
}, (name?: string): string => {
    if (!name) {
        return 'Hello World!';
    }
    return `Hello ${name}!`;
});

server.post('/jrpc', async (request, reply) => {
    const {body} = request;
    const result = await jrpcServer.executeRequest(JSON.stringify(body));
    reply.send(result);
})
...

For the client:

...
export interface DemoRpcMethods {
    hello(name?: string): Promise<string>;
}

(async () => {
    ...
    const client = new JRPCClient<DemoRpcMethods>("http://127.0.0.1:3000/jrpc", resolver);
    const proxy = client.createProxy();
    console.log(await proxy.hello());
    console.log(await proxy.hello('Demo'));
})();

As you can see from the client code, it is enough to define an interface that describes the RPC methods, and the library takes care of the rest, eliminating the need to manually define the request payload.

Using the CLI allows you to automatically generate the interface from the schema produced by the server, further simplifying the process.

For more details, visit my GitHub to see the full demo code.

Conclusion
#

In conclusion, JRPC is my contribution to the community of developers working with JSON RPC. I firmly believe that collaboration is the key to developing high-quality software, and for this reason, I invite everyone to contribute to the project. Whether you have an idea for a new feature, a bug report, or a fix, every contribution is valuable and makes a difference.

Marco Ferraioli
Author
Marco Ferraioli
Platform Architect with a passion for building innovative solutions