nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.28k stars 7.59k forks source link

GRPC-Duplex client cannot send messages to the server #3906

Closed sjkummer closed 4 years ago

sjkummer commented 4 years ago

Bug Report

Current behavior

When creating GRPC duplex stream methods on client side, nestjs does not provide a way for the client to send messages to the server.

Input Code

Internally, nestjs creates a ClientDuplexStream object inside the createStreamServiceMethod of client-grpc.ts (named call) but there is no way to access it. https://github.com/nestjs/nest/blob/master/packages/microservices/client/client-grpc.ts#L116

After doing a hack and makeing the ClientDuplexStream accessable, messages can be successfully sent to the server. Therefore, the underlying GRPC-mechanisms are working fine.

Expected behavior

The GRPC-Client declaration should ether return a ClientDuplexStream e.g.:

export interface ExampleService {
  duplexMethod(metadata: grpc.Metadata): ClientDuplexStream<Request, Response>;
}

or accepting upstream Observables, e.g.:

export interface ExampleService {
  duplexMethod(upstream: Observable<Request>, metadata: grpc.Metadata): Observable<Response>;
}
sjkummer commented 4 years ago

I found a good solution for my GRPC problems with Nestjs.

For everyone wondering: You can ignore the poorly documented Nestjs method wrappers and use plain GRPC instead, e.g.:

@Client(grpcClientOptions)
private client: ClientGrpc;
const client = this.client.getClientByServiceName("MyService");
const method: ClientDuplexStream<Request, Response> = client["MyDuplexMethod"](metadata);

method.on('data', chunk => {

});

method.on('error', (e) => {

});

method.on('end', () => {

});

method.write(new Request());
kamilmysliwiec commented 4 years ago

Fixed + added docs + updated sample :) ref https://docs.nestjs.com/microservices/grpc#streaming-sample

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.