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

How to use with grpc.Server? #1381

Open
dinfer opened this issue Mar 25, 2020 · 8 comments
Open

How to use with grpc.Server? #1381

dinfer opened this issue Mar 25, 2020 · 8 comments

Comments

@dinfer
Copy link

dinfer commented Mar 25, 2020

I need to dynamically bind functions to GRPC services, but I cannot find document to describe how to do that.

The wiki page API Document link is broken. (the link)
The example in the README is not used with grpc.Server

    const svc = new protobuf.Service(cfg.name);

    const stub = svc.create((method, requestData, callback) => {
      console.log(">>> stub", method, requestData, callback);
      // call a certain function depending on method
    });

    const server = new grpc.Server()
    server.addService(stub, stub) // 
@jsbrucker
Copy link

@dinfer
re: API Docs
Try accessing the docs here: https://protobufjs.github.io/protobuf.js/ or if running locally use:
npm run docs and launch from docs/index.html

@robert-cronin
Copy link

robert-cronin commented Jul 10, 2020

There doesn't appear to be any good documentation on this in the API Documents or README, The example in the README is for a client object. I am looking for a way to do this as well.

@murgatroid99
Copy link
Contributor

protobuf.Service has no relationship to grpc.Server. They are not compatible at all. I recommend instead using @grpc/proto-loader, which wraps Protobuf.js.

After loading the .proto files using that library, you would use it with code that looks something like this:

const serviceDefinition = packageObject.package.names.ServiceName.service;
const server = new grpc.Server();
server.addService(serviceDefinition, serviceImplementation);

@robert-cronin
Copy link

Thank you @murgatroid99 i think that answers the original question then.

@trajano
Copy link

trajano commented Oct 26, 2020

If you're using proto-loader does that mean we can't use pbts to generate the type file? https://stackoverflow.com/questions/64530776/how-do-you-generate-typings-for-protobuf-files-for-use-with-grpc

@murgatroid99
Copy link
Contributor

That's right. The code generated by @grpc/proto-loader does not use objects of the same types that are generated by pbts. There is ongoing work in the PR grpc/grpc-node#1474 to address that issue by adding a TypeScript generator in @grpc/proto-loader itself.

@trajano
Copy link

trajano commented Oct 26, 2020

So for the time being I guess we have to work without proper types. I haven't done it for a while so I can't really remember how, but is it possible for you @murgatroid99 to provide a link to a .d.ts file that we can use as a way to bypass the type checks even if it is a simple

interface somegrpcthingIMade {
  any
}

@murgatroid99
Copy link
Contributor

The PR I linked has been published as a prerelease version of @grpc/proto-loader with the tag generator-draft. So you can try it out with npm install @grpc/proto-loader@generator-draft.

If that doesn't work for you, I don't know what specific type check you want to bypass, but in general if a type definition isn't working for you you can just typecast it yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants