question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Outputting `service` for @grpc/grpc-js

See original GitHub issue

Hey, is there currently any way of outputting service definitions as code? Basically --ts_proto_opt=outputServices=grpc-js from https://github.com/stephenh/ts-proto but for this package.

Currently I’m using a mix of protobuf-es and ts-proto where ts-proto is used for our gRPC server. Would it be possible with some extensions or transpiler if it isn’t possible out of the box?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
timostammcommented, Oct 21, 2022

Yes we do, Sebastian. I hope to share our roadmap and plans along with the first public commits for connect-node.

3reactions
jellydncommented, Nov 12, 2022

There is no built-in feature to generate service definitions that are compatible with @grpc/grpc-js, but something quite similar. In protobuf-es, services are represented as a ServiceType - an object with a name and a set of methods, each with information about the streaming type, and the type of the input and output message.

For example, let’s take the following service definition in protobuf:

syntax="proto3";
package demo;

service ElizaService {
  // Say is a unary request demo. This method should allow for a one sentence
  // response given a one sentence request.
  rpc Say(SayRequest) returns (SayResponse) {}
}

// SayRequest describes the sentence said to the ELIZA program.
message SayRequest {
  string sentence = 1;
}

// SayResponse describes the sentence responded by the ELIZA program.
message SayResponse {
  string sentence = 1;
}

The corresponding ServiceType would be:

const ElizaService: ServiceType = {
  typeName: "demo.ElizaService",
  methods: {
    say: {
      name: "Say",
      I: SayRequest,
      O: SayResponse,
      kind: MethodKind.Unary,
    },
  }
};

@bufbuild/protoc-gen-es does not generate anything for services, but @bufbuild/protoc-gen-connect-web does. It happens to generate pretty much the example above, and @bufbuild/connect-web uses TypeScript’s mapped types to turn it into a client:

import {createPromiseClient} from "@bufbuild/connect-web";

const client = createPromiseClient(ElizaService, ...);
client.say({ sentence: "Hello" }); // this is type-safe

The fun part is that you can simply use @bufbuild/protoc-gen-connect-web to generate service types, and turn them to @grpc/grpc-js clients and servers. We’re making sure that this works in unit tests here and here. So connecting a few dots, you end up with a @grpc/grpc-js client:

import * as grpc from "@grpc/grpc-js";

const client = createGrpcClient(ElizaService, { 
   address: "localhost",
   channelCredentials: grpc.ChannelCredentials.createInsecure()
});

client.say({ sentence: "Hello" }, (err: grpc.ServiceError | null, value?: SayResponse) => {
  //       
});

This is just a proof of concept, but it should work pretty well. If you want to give it a try, let us know if you encounter any problems!

Just FYI - This approach is working. I’ve implemented a simple on my monorepo just in case if someone need this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basics tutorial | Node - gRPC
This tutorial provides a basic Node.js programmer's introduction to working with gRPC. By walking through this example you'll learn how to:.
Read more >
Tutorial on How to Implement gRPC Services in NodeJS
So, in this tutorial, we will implement gRPC services in NodeJs. We will build a demo application and perform a CRUD operation.
Read more >
Building a gRPC Client and Server with Node.js
gRPC works with the service. Service is just a method that can be called remotely with parameters and return types.
Read more >
Tutorial: How to Build Your First Node.js gRPC API - Trend Micro
This article will demonstrate how to develop a basic Node.js gRPC API that salts and hashes a password received from a client endpoint....
Read more >
gRPC for Web Clients - GitHub
Define your service using protocol buffers · Implement a simple gRPC Service using NodeJS · Configure the Envoy proxy · Generate protobuf message...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found