Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gRPC metadata example #115

Closed
kamilmysliwiec opened this issue Jul 21, 2018 · 16 comments
Closed

Add gRPC metadata example #115

kamilmysliwiec opened this issue Jul 21, 2018 · 16 comments

Comments

@kamilmysliwiec
Copy link
Member

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request (new chapter/page)
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Expected behavior

As a user, I would like to see passing gRPC metadata example

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

@dzzzzzy
Copy link

dzzzzzy commented Oct 16, 2018

I have two application, the one is an graphql api application for frontend, the other is a microservice using grpc.

How can I pass the request instance to the rpc call, because I want to use it to show these request trace.

Or is there any way to trace the whole req/res chain?

@kamilmysliwiec Hello kamil, is there any solution for my issue? I am waitting for your reply, thanks a lot!

@woodcockjosh
Copy link

@kamilmysliwiec Is this related to this nestjs/nest#747? Will this issue resolve the need to have an example with a separated client and server instead of some hybrid thingy which calls itself?

@josh9060
Copy link

josh9060 commented Nov 4, 2019

Is metadata implemented in nest? Or is it a feature to be added?

If it is implemented, is there any docs on how to use it within nest?

@kamilmysliwiec
Copy link
Member Author

@Joshgallagher it's implemented. There are no docs yet though :(

@josh9060
Copy link

josh9060 commented Nov 4, 2019

@kamilmysliwiec can you point me in the direction of a resource or some sort of rough implementation?

I can make a PR to examples with an example using metadata.

@josh9060
Copy link

josh9060 commented Nov 4, 2019

Also, to clarify:

  1. Can we create metadata from a client and send it to the gRPC service?
  2. Can we create metadata from the gRPC service itself, for use on the client?

@josh9060
Copy link

josh9060 commented Dec 9, 2019

@kamilmysliwiec Any updates? I'm using MaliJS until metadata is documented in Nest.

@Davide-Gheri
Copy link

Davide-Gheri commented Jan 8, 2020

@Joshgallagher To pass metadata from client to service I found that you need to pass a grpc.Metadata instance as second parameter to the client service call, it doesn't work with plain objects.

const meta = new grpc.Metadata();
meta.set('context', JSON.stringify(ctx));
this.client.getService('SomeService').someMethod(payload, meta);

it seems that the opposite (meta from service to client) is not supported
https://github.com/nestjs/nest/blob/master/packages/microservices/server/server-grpc.ts#L210
I think this is a grpc.sendUnaryData callback that accepts a grpc.Metadata instance as third argument

@sjkummer
Copy link

sjkummer commented Jan 9, 2020

I have a nestjs instance acting as GRPC-Client sending metadata to another nestjs instance that is acting as a GRPC-Server as you described.
But: Seems like the metadata got lost or overwritten somehow.

Edit:
The example above works for me too. Just make sure to use the official grpc.Metadata from https://www.npmjs.com/package/grpc and not accidentally import it from another package.

@guyisra
Copy link

guyisra commented Mar 3, 2020

Is there an update or a roadmap on this (adding metadata on the service)?

@kerf007
Copy link

kerf007 commented Jun 25, 2020

I have similar issue. I'm as grpc client have to add jwt token to each request to grpc server and I don't understand how to do it. Help please :)

@josh9060
Copy link

josh9060 commented Oct 2, 2020

@kamilmysliwiec Is there any update on the docs providing information on gRPC (nest) microservices returning Metadata?

@sjkummer
Copy link

sjkummer commented Oct 2, 2020

@Joshgallagher I'll recommend to have a look at https://github.com/stephenh/ts-proto and use their NestJS support: https://github.com/stephenh/ts-proto/blob/master/NESTJS.markdown

You'll get all the messages and gRPC call stups generated - including metadata option.

@josh9060
Copy link

josh9060 commented Oct 2, 2020

@sjkummer from my quick glimpse at that package is doesn't add support for gRPC metadata from the server. Correct me if I am wrong though.

@sjkummer
Copy link

sjkummer commented Oct 2, 2020

@Joshgallagher Nestjs already supports gRPC metadata - it's just not well documented, how to declare the methods. You can use generated stubs from ts-proto or write your own declarations by following https://docs.nestjs.com/microservices/grpc

@kwado-tech
Copy link

kwado-tech commented Jan 22, 2022

This doc illustrates the use of ts-proto package to generate ts files from .proto schemas.

here is an example script to generate for user.proto schema

# nestJs - /gen-nestjs-proto.sh
generate_nestjs_proto_definitions() {
    # remove directory if exists
    rm -r ./src/proto/nestjs

    # make directory
    mkdir ./src/proto/nestjs

    # generate definitions
    protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=addGrpcMetadata=true --ts_proto_opt=nestJs=true --ts_proto_out=./src/proto/nestjs ./src/proto/user.proto
}

@nestjs nestjs locked and limited conversation to collaborators Jan 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants