🦅 NestJS Advanced

How do you implement gRPC in NestJS?

Answer

gRPC is a high-performance RPC framework using Protocol Buffers for serialization. NestJS supports it as a microservice transport. Create a gRPC microservice: NestFactory.createMicroservice(AppModule, { transport: Transport.GRPC, options: { url: '0.0.0.0:5000', package: 'hero', protoPath: join(__dirname, 'hero.proto') } }). Controllers use @GrpcMethod('HeroesService', 'FindOne') instead of HTTP decorators. The method name maps to the service definition in the .proto file. For the client side, use ClientGrpc to get a service client and call its methods. gRPC uses HTTP/2 for multiplexing and bidirectional streaming, making it significantly more efficient than REST for inter-service communication in high-throughput microservice systems.