nestjs / docs.nestjs.com

The official documentation https://docs.nestjs.com 📕
MIT License
1.19k stars 1.71k forks source link

Request for example of gRPC client streaming #575

Closed danielkerwin closed 4 years ago

danielkerwin commented 5 years ago

Hello,

I am having trouble understanding how to correctly implement streaming when using a grpc client instance.

My Nest app is HTTP however it needs to connect to, and communicate with an external service via gRPC thus we are not performing app.connectMicroservice and not using @Controller for RPC methods.

The client instance exposes several streaming methods but they cannot be used directly and so would like to ask advice on how to wrap/use them in a stream:

Dynamic config provided to feature module:

  {
    provide: GRPC_CONFIG_TOKEN,
    useFactory: async (config: ConfigService): Promise<ClientProxy> => {
      return ClientProxyFactory.create({
        transport: Transport.GRPC,
        options: {
          url: config.grpcUrl,
          package: 'some-package',
          protoPath: join(__dirname, '../../../../proto/protos.json'),
        },
      });
    },
    inject: [ConfigService],
  },

Token injected and retrieve the appropriate service:

constructor(@Inject(GRPC_CONFIG_TOKEN) private grpcClient: ClientGrpcProxy) {
    this.grpcService = grpcClient.getService<ServiceAPI>('some-service');
}

Method that is exposed (proto) rpc SomeMethod(stream SomeData) returns (google.protobuf.Empty) {}

Which is interpreted to the following typescript: public someMethod(request: SomeDataType): Observable<google.protobuf.Empty>;

How then to use?:

constructor(...) {}

async myServiceMethod() {
   // this fails since it does not stream
   await this.grpcService.putFile('some data').toPromise();
}
kamilmysliwiec commented 4 years ago

https://docs.nestjs.com/microservices/grpc#streaming-sample