0PricingLogin
NestJS Enterprise Backend APIs · Lesson

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 @GrpcStreamMethod for 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 users maps to the transport option package: 'users'.
  • FindOne is 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

  1. Defining Services and Messages in Protobuf
  2. Implementing and Consuming gRPC Methods
  3. Streaming RPCs and Backpressure
  4. Contract Evolution and Backward Compatibility
← Back to NestJS Enterprise Backend APIs