Building a gRPC Client and Server with Node.js

Aatif Bandey
JavaScript in Plain English
4 min readJun 24, 2021

--

A few months ago, I was working on a project where I was asked to make use of gRPC to fetch some data. For me, it was the first time I heard about this term gRPC.

When in doubt, google !

I reached out to a few folks but didn’t get much help. I Googled about gRPC implementation in Node.js but was not able to find the right source with a detailed implementation.

After a lot of research and with few resources available online, at last, I was able to finish my job.

In this article, I will share how you can set up gRPC server and client with Node.js.

What is gRPC?

gRPC is Google Remote Procedure Call, it’s simply a protocol to fetch or post data like REST. If you want to know more about gRPC you can read it here.
I will not talk about the benefits of using gRPC over REST or vice-versa.

Source: https://grpc.io/

Concept

gRPC works with the service. Service is just a method that can be called remotely with parameters and return types. We can define our service in the .proto file.

salary.proto

In the proto file, I have defined two messages EmployeeRequest and EmployeeResponse. In the EmployeeRequest , we accept one field called employeIdList which can be an array of integers and EmployeeResponse we have a field called message.

I have defined a service named Employee it has one rpc function paySalary to check if the salary has been paid or not.

Let’s assume my backend server is already set up and I just need to make a call and get the response.

To set up gRPC on the client, I would need to install a few dependencies to get it running.

- @grpc/grpc-js
- @grpc/proto-loader
client.js

As mentioned above gRPC works with service which is defined in the .proto file. In the above snippet, we are loading our .proto file with loadSync method and we pass the output of loadSyncto gRPC’s loadPackageDefinition.

Request to a gRPC server

Let’s check how to make a connection to a remote server and fetch data.

In the above snippet, I have defined a function named main which is responsible to make a connection to the server with no authentication.
I am passing a dummy list of employee id employeIdList:[] to our RPC function paySalary and after a successful connection and client request, we get the response in the callback 😃.

This is it 🤩. A minimum setup is required to fetch data from a remote gRPC server.

Now let’s check out how to create a gRPC server with Node.js.

Server setup

Before going into the details of each component required to create a server, let’s check the implementation first.

I already have dependencies installed. But there are few important methods required to create, start and keep the server running.

Following is the list:
server() It will create the server.
bindAsync() will specify the address and port to listen to the client requests coming in, and in the callback, I am starting the RPC server.
addService() It will add the service with the corresponding implementation.

pay_salary.js

In the above snippet, I am defining the method implementation which is passed to the service. It receives the data employeeIdList from client request, process it, and send the response back to the client.

My server is able to listen to the client's request and sends the response back.
Yeah! We are done! 😃😃😃😃 Let’s celebrate!

I have shared the minimum setup required to create and start a server with Node.js and to create a gRPC client that sends the request to the gRPC server.
This article will give you the start to explore more on gRPC and its implementation.

I hope you enjoyed reading this article.

If you liked this, be sure to follow me on Twitter!

References

https://grpc.io/docs/languages/node/basics/

More content at plainenglish.io

--

--