DEV Community

Yoshiyuki Kato
Yoshiyuki Kato

Posted on • Edited on

18 4

GRPC on Node.js more simply

When developing GRPC application on Node.js, we can use grpc and @grpc/proto-loader. Though these nice packages provides unified APIs to build GRPC server and client, they are too primitive for me to use directly in my application code. I want more simplified API such as createServer and createClient.

So, I am developing grpc-kit. It is a small wrapper library of grpc and @grpc/proto-loader. Ofcourse it provides createServer and createClient😆

Showing an example bellow.

greeter.proto

syntax="proto3";

package greeter;

service Greeter {
  rpc Hello (RequestGreet) returns (ResponseGreet) {}
  rpc Goodbye (RequestGreet) returns (ResponseGreet) {}
}

message RequestGreet {
  string name = 1;
}

message ResponseGreet {
  string message = 1;
}
Enter fullscreen mode Exit fullscreen mode

server.js

const {createServer} = require("grpc-kit");
const server = createServer();

server.use({
  protoPath: "/path/to/greeter.proto",
  packageName: "greeter",
  serviceName: "Greeter",
  routes: {
    hello: (call, callback) => {
      callback(null, { message: `Hello, ${call.request.name}` });
    },
    goodbye: async (call) => {
      return { message: `Goodbye, ${call.request.name}` };
    }
  }
});

server.listen("0.0.0.0:50051");
Enter fullscreen mode Exit fullscreen mode

createServer returns an instance of wrapper class of grpc.Server. The class provides use method for adding a service to provide. You can declaratively configure the service with a routing map. The routing map consists of pairs of method declared in greeter.proto and handler function (both of sync/async functions are available 😎). After the service configured, it starts server by listen method.

client.js

//client.js
const {createClient} = require("grpc-kit");
const client = createClient({
  protoPath: "/path/to/greeter.proto",
  packageName: "greeter",
  serviceName: "Greeter"
}, "0.0.0.0:50051");

client.hello({ name: "Jack" }, (err, response) => {
  if(err) throw err;
  console.log(response.message);
});

client.goodbye({ name: "John" }, (err, response) => {
  if(err) throw err;
  console.log(response.message);
});
Enter fullscreen mode Exit fullscreen mode

createClient returns an instance of grpc.Client. It calls methods declared in greeter.proto and receives a response in callback.

In detail about API, please check out the repository. The library is in developing now. Any comments and advices are welcome. Thank you!

GitHub logo YoshiyukiKato / grpc-kit

Use grpc more simply on Node.js

grpc-kit

npm version

Use grpc more simply on Node.js.

quick start

install

$ npm install grpc @grpc/proto-loader grpc-kit
Enter fullscreen mode Exit fullscreen mode

proto

syntax="proto3";

package greeter;

service Greeter {
  rpc Hello (RequestGreet) returns (ResponseGreet) {}
  rpc Goodbye (RequestGreet) returns (ResponseGreet) {}
}

message RequestGreet {
  string name = 1;
}

message ResponseGreet {
  string message = 1;
}
Enter fullscreen mode Exit fullscreen mode

Server

const {createServer} = require("grpc-kit");
const server = createServer();
server.use({
  protoPath: "/path/to/greeter.proto",
  packageName: "greeter",
  serviceName: "Greeter",
  routes: {
    hello: (call, callback) => {
      callback(null, { message: `Hello, ${call.request.name}` });
    },
    goodbye: async (call) => {
      return { message: `Goodbye, ${call.
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay