Implementing and Consuming gRPC Methods
Wire @GrpcMethod handlers and ClientGrpc proxies for unary request/response calls.
Unary gRPC in NestJS
gRPC services in NestJS are defined by a .proto contract and implemented as ordinary providers decorated with gRPC handler metadata. The most common interaction is the unary call: the client sends a single request message and receives a single response message.
- The server exposes handlers via
@GrpcMethod(or@GrpcStreamMethodfor streams). - The client obtains a typed proxy through
ClientGrpc.getService()and calls methods that return Observables.
In this lesson we wire both ends of a unary request/response flow for an enterprise-style UsersService.
The .proto contract
Everything starts with the service contract. The package name and the service / rpc names are what NestJS uses to bind handlers and to resolve the client proxy.
package usersmaps to the transport optionpackage: 'users'.FindOneis the RPC NestJS will route to a matching handler.
syntax = "proto3";
package users;
service UsersService {
rpc FindOne (UserById) returns (User) {}
}
message UserById {
int32 id = 1;
}
message User {
int32 id = 1;
string name = 2;
string email = 3;
}All lessons in this course
- Defining Services and Messages in Protobuf
- Implementing and Consuming gRPC Methods
- Streaming RPCs and Backpressure
- Contract Evolution and Backward Compatibility