Getting started with gRPC and NodeJS

Chetan Pandey
3 min readSep 10, 2020

Today we are going to learn the basics of developing gRPC based service by creating a simple example login service using NodeJS, and Express.

gRPC Remote Procedure Calls

What is gRPC?

gRPC is an open Source RPC (Remote Procedure Call)framework developed by Google. In the world of REST API, the first question that comes to mind why use gRPC when we have REST API, It is easy to build, It supports many data formats, it is stateless. But when we talk about the microservices architecture, load balancing, high scalability, low latency between service interfaces gRPC comes forward as a better option than REST.

With gRPC, we can create services that are distributed across different machines, different regions and the client can easily across different machines and platforms. gRPC uses Protocol Buffer which is Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data. gRPC support following RPC

Unary RPC: This is the Simplest RPC available. Here the Client sends a Request message to the Server. The Server processes the request and then sends a Response message back to the Client.

Server Streaming RPC: In this RPC, the client sends a request message to the server, and the server sends a sequence of messages back to the Client in a stream fashion.

Client Streaming RPC: In this RPC, the client sends a sequence of messages to the server in a stream fashion. The server then processes all these requests and then sends a response message back to the client.

Bidirectional Streaming RPC: In this RPC, the client sends a sequence of messages to the server in a stream fashion. The server then processes the request and then sends a sequence of messages back to the client in a stream fashion.

We will focus on Unary RPC and develop a simple gRPC login service using nodeJS and Express. Which has a REST interface and a gRPC interface for inter-service communication.

Requirements:

  • Node.js and npm (latest versions)
  • The IDE of your choice

Let's initialize an npm project and install the following dependencies

npm install --save grpc @grpc/proto-loader express body-parser

The directory structure for the code is like this:

First, let's create a proto definition for LoginService:

Here this syntax = “proto3”; refers to Protocol Buffer version 3. This file will create Proto for login service.

Now we will create the login gRPC server:

In the above code require(“grpc”) is the npm package for the gRPC server and require(“@grpc/proto-loader”) is the npm package for the protocol buffer npm package.

To access the gRPC server we need to create a gRPC client.

loginClient will be used to access the login server by the REST service.

In last we have to create the REST interface using express like this:

To run loginService execute the following commands:

~/loginService/server
$ node loginServer.js
~/loginService/server
$ node loginServer.js

Code:

The code for this article is available at https://github.com/Chetan177/loginService

--

--

Chetan Pandey

A Software Engineer from New Delhi, India with interests in Data Science, GoLang, NodeJS, Python Development, Microservices, APIs, VOIP, Neural Networks, UI/UX