protobufjs / protobuf.js

Protocol Buffers for JavaScript & TypeScript.
Other
9.93k stars 1.42k forks source link

How to use with grpc.Server? #1381

Open dinfer opened 4 years ago

dinfer commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

trajano commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.