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?
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.