×

Search anything:

What is tRPC and why should you use it?

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article, we are going to dive into the world of tRPC, discussing what is tRPC, why should you use and when should you use.

What is tRPC?

In summary, tRPC is an alternative to REST, GraphQL, gRPC and others remote procedure call. It’s a way of creating type safe API without the need of using a codegen tool, and as it is not a full language-agnostic as GraphQL it uses the full potential of typescript or JavaScript frameworks.

Why do I need tRPC?

It is a easier choice for when you are creating an API communication between the backend and frontend, this communication will be type safe, light, simple and performative, and it’s readable. It can be consumed in different environments such as mobile. And the

On the newer version of tRPC (x10) you can even make a server with it, so no need of express and setting res.json().

You can define the procedure that you are going to use, and use multiple procedures in router. A procedure is a function that will be executed on a remote system over a network, so you define the procedure on the server and the client will be calling it when a request that has this procedure is made. The procedures you are going to make can have different purposes, but it has great utility on tRPC.

tRPC have some great quick start packages on their docs for when you are first creating something with it, so to set up your app is really quick and easy, and there are a lot of choices for your developer taste.

When should I use tRPC?

With this article I don't want to convince you to use tRPC on every project you make or migrate every API communication to tRPC, every framework, tool or remote procedure call have its own advantages, disadvantages and use cases.

If you want to create a full stack application with typescript, I would recommend tRPC because of its great compatibility and connection with the language, if you are already familiarized with zod, or wants to start using it, tRPC is a great choice too.

Let’s start by talking about the type safety of tRPC, when creating an API it’s very important to ensure that the data been received or sent is in a correct format, so that the database does’t receive wrong data and is being use as a type validator. tRPC can make sure that the data transmitted is in the correct form and the dev team even recommend using zod for simplicity’s sake.

Creating a new project is really simple, there is no need for code generation or complicated step-by-step guides, just yarn create some of the example apps of the documentation and start coding, with tested and production ready apps.

It also has request batching, combining multiple requests in one single request and the server responds in one single batch response. This is really useful for reducing the network congestion and improving the performance of the network communication.

tRPC also have subscription support, which means that the API has real-time bidirectional communication, this means that the client can receive real-time data updates on whatever it’s needed, similar to a web socket.

This API is also a lightweight option to gRPC, it has little client-side footprint, smaller codebase, and performative while using fewer resources compared to gRPC.

With this article at OpenGenus, you must have a good basic idea of tRPC.

What is tRPC and why should you use it?
Share this