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.
Previous
What is the CQRS pattern and how does NestJS support it?
Next
What is the NestJS Interceptor execution order and how do multiple interceptors interact?
More NestJS Questions
View all →- Advanced What is the CQRS pattern and how does NestJS support it?
- Advanced What is the NestJS Interceptor execution order and how do multiple interceptors interact?
- Advanced How does NestJS handle graceful shutdown?
- Advanced What is custom provider syntax in NestJS (useClass, useValue, useFactory)?
- Advanced What is the difference between forRoot() and forFeature() in NestJS modules?