Creating your gRPC service in Nodejs/Typescript 🔥 (Part 1)

TDot Code
4 min readJun 7, 2021

--

Hello everyone! As usual if you’re interesting in watching a video tutorial for this you can open the link below!

Now since I got that shameless plug out of the way lets move on to the point of this article!

What is gRPC?

gRPC stands for (Google)Remote Procedure Call and leverages Protocol Buffers for their means of trasfering data over HTTP 2. This is crutial, the only way you’re making direct communication to a gRPC server is through HTTP 2 and with some extra control over the TCP stack. Which is one of the reasons its super difficult to implement a direct communication through a regular modern browser.
So pretty much you can think of Protocol Buffers as JSON because its the way the data is serialized and gRPC just allows the form of communication while providing features.

gRPC is all the hype these days, its the new kid on the block that provides amazing performance due to the blazing fast serialization and a very compact payload. So lets get started!

What do we plan to create in this article?

  • Create a gRPC server in node & typescript
  • Create a gRPC client in node & typescript
  • Create various services that can be used by the ccclient
  • Create 4 types of gRPC calls
  • Unary, Client Stream, Server Stream and Bi-directional Streaming

Setting up our Project

You can use npm or yarn, in my case I’m going to be using yarn to setup my project. Below are the following commands

yarn init -y
yarn add typescript ts-node @grpc/grpc-js @grpc/proto-loader

@grpc/grpc-js is what will help up create our client and server and @grpc/proto-loader is whats going to be helping us load our .proto files

After the project folder is setup create two files server.ts, client.ts and a folder called proto. The proto folder is where we will be defining our payloads and and services. Now in your proto folder create a file with a .proto extension. In my case I named it random.proto due to the random services I’m going to be creating. My proto file looks like this below

random.proto

Above we defined our syntax, and package name(randomPackage) and a service called Random. Inside the service we can defined our various rpc calls that a client can perform. In this case I create a simple rpc called PingPong where it takes a PingRequest from the client and responds to the client with a PongRequest they both have a payload with a key called message that is of type string. Isn’t this really easy to read and understand? 😲

Now for TYPESCRIPT!!! For me, I created a proto-gen.sh file in the root folder and I added this line of code into it

#!/bin/bashyarn proto-loader-gen-types --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto

Essentially what this is doing is parsing the already created random.proto file and creating type definitions according to it. It outputs all needed typescript definitions into the same proto folder under a folder named according to the package name, in my case it was randomPackage.

Now we can start adding typescript code to our server.ts file

Here is what your server.ts file should look like after. Most of everything you see here is boilerplate code. The part that should concern you the most is between lines 29–33. This is where you add your business logic to your various rpc calls. Here we are simply responding to an rpc call with a message Pong. Now lets look at what our client code looks like!

client.ts

Again most of this is boilerplate code and doesn’t really matter explaining what's going on. What you should notice is the means of interaction with the server. Unlike regular rest we call a named endpoint. But in gRPC case its as easy as instantiating a client and calling the rpc by its name PingPong 😲 HOW EASY IS THAT?

Lets testing things out, I added some helper scripts to my package.json for the server and client.

package.json

We can now start our server by calling yarn start we should get an output in our terminal signaling that the server has been setup correctly, finally we start our client by calling yarn client. You should see an output in the client terminal like this

This is an example of a unary call, it can be compared to everyday rest calls that send a payload and close the connection as soon as the sever responds with data. Now lets move on to more fun calls like streaming that come out of the box with gRPC 😀

In the next article we are going to creating our Server and Client Streaming calls, which I find more fun actually and bet you would aswell. So stick around and find out how we leverage streaming calls 😊

Thanks for reading!
Github Code:
https://github.com/floydjones1/ts-node-grpc
Personal Website: https://tdotcode.com/

--

--

TDot Code
TDot Code

Written by TDot Code

Just a software developer with a youtube channel! If you’re interested drop a follow :) https://www.youtube.com/channel/UCUwA7VxRo-uw2eQJ52EkKlQ

No responses yet