loading...

GRPC on Node.js more simply

yoshiyukikato profile image Yoshiyuki Kato Updated on ・2 min read

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;
}

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");

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);
});

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!

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

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;
}

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.request.name}`

Discussion

pic
Editor guide