Overview

Build high-performance gRPC services with proto file support and example controller implementation.

Features

  • Proto file loading
  • gRPC service options
  • Example controller patterns
  • Streaming support

Installation

npx hanma add nest-grpc --framework nest

Dependencies

@nestjs/microservices, @grpc/grpc-js, @grpc/proto-loader

Code

import { MicroserviceOptions, Transport } from "@nestjs/microservices";
import { join } from "path";

export const grpcClientOptions: MicroserviceOptions = {
  transport: Transport.GRPC,
  options: {
    package: "example", // Proto package name
    protoPath: join(__dirname, "../proto/example.proto"),
    url: process.env.GRPC_URL || "0.0.0.0:5000",
    loader: {
      keepCase: true,
      longs: String,
      enums: String,
      defaults: true,
      oneofs: true,
    },
  },
};

// Example proto file content (save as proto/example.proto):
/*
syntax = "proto3";

package example;

service ExampleService {
  rpc FindOne (FindOneRequest) returns (ExampleResponse);
  rpc FindMany (FindManyRequest) returns (stream ExampleResponse);
}

message FindOneRequest {
  string id = 1;
}

message FindManyRequest {
  repeated string ids = 1;
}

message ExampleResponse {
  string id = 1;
  string name = 2;
  string createdAt = 3;
}
*/

// Example controller implementation:
/*
import { Controller } from "@nestjs/common";
import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices";
import { Observable, Subject } from "rxjs";

interface FindOneRequest {
  id: string;
}

interface ExampleResponse {
  id: string;
  name: string;
  createdAt: string;
}

@Controller()
export class ExampleGrpcController {
  @GrpcMethod("ExampleService", "FindOne")
  findOne(data: FindOneRequest): ExampleResponse {
    return {
      id: data.id,
      name: "Example",
      createdAt: new Date().toISOString(),
    };
  }

  @GrpcStreamMethod("ExampleService", "FindMany")
  findMany(data: Observable<FindOneRequest>): Observable<ExampleResponse> {
    const subject = new Subject<ExampleResponse>();
    
    data.subscribe({
      next: (req) => {
        subject.next({
          id: req.id,
          name: "Streamed Example",
          createdAt: new Date().toISOString(),
        });
      },
      complete: () => subject.complete(),
    });
    
    return subject.asObservable();
  }
}
*/

Usage

// Add to microservice options
app.connectMicroservice(grpcClientOptions);

Dependencies

Frequently Asked Questions

How do I install gRPC Server?
Run the following command: npx hanma add nest-grpc --framework nest
What dependencies are required?
This snippet requires: @nestjs/microservices, @grpc/grpc-js, @grpc/proto-loader. Make sure to install them before using the snippet.
How do I use this snippet?
Copy the code and paste it into your project. The snippet will be saved to src/grpc/grpc.options.ts.
Can I customize this snippet?
Yes! All snippets are copy-paste code that you own. Feel free to modify them to fit your needs.
Does this work with other frameworks?
This snippet is designed for the specific framework. Check the other framework pages for equivalent implementations.