What is RPC and how you can use it in NestJS is the question I had in mind to answer in this post. Sit tight for a short, concise, and precise intro to this world:
So RPC,
- Stands for Remote Procedure Call.
- It's a communication mechanism.
- It's almost the same as though we have access to the methods/function defined in another program.
- Can be used for inter-service communications.
- Supper efficient.
[!CAUTION]
This is still a network call. So do not call it like your codes who are living in the same codebase.
[!NOTE]
We can use any application level protocol for sending requests and receiving responses:
- TCP: HTTP1, HTTP2.
- UDP.
- Websockets.
- etc.
This means that it is like GraphQL transport layer agnostic. You can use whatever you like.
gRPC
Now let's demystify the gRPC. It's:
- An open-source rewrite of RPC used by Google.
- Why gRPC?
- It uses only HTTP2 as its transport layer.
- Protocol buffer as our data exchange format.
- No browser support yet, but a good thing to have for inter-service communication in a microservice architecture.
NestJS
I love NestJS and how good it is at giving you're code more structure. So here is how you can get up to speed with it.
- Follow the official documentation opn how to bootstrap a NestJS app + gRPC.
pnpx create-nx-workspace grpc --preset=nest --appName nestjs-client --bundler esbuild --packageManager pnpm --nxCloud skip
- I also love perfectionist for a good reason and that is the tidiness it gives you for little conf, so I highly recommend you take a look at it in their official doc.
Or if you're looking for something on how to get up to speed I guess this commit can be a real quick intro to how to use it. BTW I appreciate it if you give my repo a star in case it was helpful 🙂.
nx run-many -t lint --fix
- Write you're protobuf file like what I did here at
user.proto
. - Use the
ts-proto
to auto generate the interfaces and decorators for your gRPC controller.
{
"scripts": {
"grpc:gen": "npx protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=fileSuffix=.interface --ts_proto_opt=nestJs=true --ts_proto_opt=addNestjsRestParameter=true --ts_proto_out={projectRoot}/src/assets/interfaces -I {projectRoot}/src/assets/ {projectRoot}/src/assets/*.proto"
}
}
For the complete solution look at this.
Now just run the
npm run grpc:gen
or if you're using Nx just runnx grpc:gen projectName
.Congrats, you have your types and decorators. So let's put them into use.
[!TIP]
For a complete example look at the
nestjs-client
.
Ref
BTW you can find a working example in this repo too, so if this post was useful to you consider sharing it, giving my repo a star and this post a like.
kasir-barati / nestjs-materials
NestJS tips, tricks, Notes, Things which are not in doc and I used to figure them out and use them
Important
Keep this file synchronized with index.md
.
nestjs-materials
NestJS tips, tricks, Notes, Things which are not in doc and I used to figure them out and use them
- Microservices
- How to debug your code and flaky tests.
- Designing and versioning RESTful APIs.
- MockServer and mocking 3rd-party HTTP/S calls.
- Kafka intro.
- RabbitMQ intro.
- NestJS and GraphQL with
nestjs-query
Instagram: https://www.instagram.com/node.js.developers.kh/
Facebook: https://www.facebook.com/kasirbarati
X: https://x.com/kasir_barati
YouTube: https://www.youtube.com/@kasir-barati
GitHub: https://github.com/kasir-barati/
Dev.to: https://dev.to/kasir-barati
LinkedIn: https://linkedin.com/in/kasir-barati
Top comments (0)