GRPC Basics & Integration Step by Step

Gamze Yılan
Nerd For Tech
Published in
4 min readJul 15, 2022

--

In this article you’ll find the basics of the GRPC and an overview on how to set up client/server microservices with it.

What is GRPC?

When working with microservices there are a lot of things to worry about, such as the type and the size of the data, the endpoint structure, the latency of the response… This can make dseveloping APIs even harder than it should be, but at it’s core, what we want to do is send a request and get a response. Hence we have the Remote Procedure Call (RPC). With RPC you’re basically calling a method from the server to get the results without having to deal with endpoints, the data type and size etc. and the server deals with all these & gives you exactly what you need.

Think of it like this: you’re sending an e-mail to your mother asking her to bake you some cookies and mail them back to you. This is at it’s core what RPC is. And since your mother won’t bake cookies for anyone other than family, we have to configure authentication to be able to send a request to the server.

Short for Google Remote Procedure Call, gRPC is an open source RPC framework by Google.

Getting Started

Google has a set of tools called Protocol Buffers. What they do is basically to take the data, serialize it into binary, and then translate it to whatever format it needs to be in. If you’re not familiar with the Protocol Buffers, I suggest you read this two minute, simplified article first.

In order to work with gRPC, we’re going to need to define the messages and services using the mentioned Protocol Buffers with a .proto file. The rest of the code will be generated automatically and all we’re going to do from that point on is to implement it.

Benefits of using gRPC can be summed as:

  • Unlike other message brokers like Apache Kafka and Redis, gRPC offers type safety. Meaning if you ask for the response as a JavaScript object then it will return a JavaScript object to you thanks yo Protocol Buffers, if you ask your mom for chocolate chip cookies she will bake you that and not some vanilla cookies.
  • Since gRPC translates data to binary and transmits it that way, it provides faster transfer.
  • Client code is auto generated and available for most languages.
  • Makes it easier to communicate between tons of microservices and get the functons via intellisense.

Using gRPC

We can now proceed to actually using gRPC, and for the purpose of this tutorial I’m going to show you how to implement gRPC on two NestJs microservices.

For each of your microservice that will work as the server you may repeat the steps below:

  • Before we get started, install @grpc/proto-loader to your project. For node you can use this link to see how to install.
  • Go to your main.ts file (or based on your language whichever file is the base) and import join from ‘path’;
import { join } from 'path';
  • Go to your equivalent of createMicroservice function and set transport to gRPC as;
transport: Transport.GRPC,
  • At this point we’ll make use of the Protocol Buffers as we’ve mentioned before. If you haven’t already, create the .proto file. A proto file is basically a simple type definition file and you can learn how to create one in two minutes by clicking here.
  • Add your .proto file as the protoPath as;
protoPath: join(__dirname, '../src/protoFileName.proto');
  • The next step is to modify our controller. Basically, go to your main controller (app.controller in NestJs) and create the method below within the constructor to map the proto file to the controller:
@GrpcMethod();
  • From this point on, make sure that all the object model within your microservice will be stored in the proto file with the correct format and implemented from there for use within each controller so that the gRPC can actually acknowledge it for translation and transmit.

Now in order to create the microservices that will work as subscribers, we must do a few changes:

  • If you already do have some message based protocols, you may now remove them from your project since you won’t need them anymore.
  • Do the steps we did for the serving microservices including installing the proto-loader for the subscribing microservices as well. The proto files and the transport & protoPath should all be exactly the same with the serving one as well.
  • At your project folder base, create a gRPC interface file (for NestJs it should be grpc.interface.ts). Inside that create an interface for the gRPC service containing the methods, and interface for the objects from the proto file as:

export interface IGrpcService{

functionName(objectName: ObjectName): Observable<any>;} //objectName should be whatever object the method takes

interface ObjectName{

… };

  • Go to your main controller and instead of implementing the GrpcMethod as we did withthe serving microservice add ClientGrpc ands IGrpcService as:

import {IGrpcService} from ‘./grpc.interface’;

import {Client, ClientGrpc } from ‘@nestjs/microservices’;

@Client(microserviceOptions);

private client ClientGrpc;

private grpcService: IGrpcService;

  • And within the same controller, inside the constructor (or ngOninit, OnModuleInit, whatever you prefer) initialize the grpc service as:

constructor(){

this.grpcService = this.client.getService<IGrpcService>(‘ServiceName’);}

And we’re done! Now you can use the functions and data objects from the grpc service like this:

this.grpcService.functionName();

--

--