DEV Community

Cover image for NestJS + gRPC + step by step guide + e2e tests

NestJS + gRPC + step by step guide + e2e tests

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.

A simple diagram showing how a NestJS app might be calling and RPC in a Python app

[!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?
    1. It uses only HTTP2 as its transport layer.
    2. 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.

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. Write you're protobuf file like what I did here at user.proto.
  2. 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"
     }
   }
Enter fullscreen mode Exit fullscreen mode

For the complete solution look at this.

  1. Now just run the npm run grpc:gen or if you're using Nx just run nx grpc:gen projectName.

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

GitHub logo kasir-barati / nestjs-materials

NestJS tips, tricks, Notes, Things which are not in doc and I used to figure them out and use them







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)