Ahmet Soner
2 min readOct 5, 2024
output1.png

If you’ve trudged through the murky swamps of web request-response protocols, you’ve probably heard of gRPC. If not, welcome aboard to Club Geek. Consider gRPC the shiny, new Cadillac, offering you a smoother ride compared to the old, groaning wagon of REST.

What gRPC Brings to The Table?

  • Speedy Gonzales: Optimized to be lightning-fast, using Protocol Buffers (Protobuf) and HTTP/2.
  • Language agnostic: It’s that one friend at the party who can talk to everyone. Supports multiple languages (Node.js included).
  • Bi-directional streaming: Channel your inner Jedi and communicate both ways in real-time.

Let’s roll up our sleeves, put on our coding goggles, (we all have a pair, right?) and dig into the action with Node.js.

First Steps with gRPC

Install the gRPC package via npm.

npm install grpc

You’ll need a .proto file, defining your service. Consider it your service’s autobiography, but much more concise. It might look something like this:

syntax = "proto3"

service SuperCoolService {
rpc SayHello (HelloRequest) returns (HelloResponse) {}
}

message HelloRequest {
string greeting = 1;
}

message HelloResponse {
string reply = 1;
}

Implementing the Server

With your .proto bibliography ready, let’s weave a server using Node.js. Consider this your digital paper mache.

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');

const packageDefinition = protoLoader.loadSync('super_cool_service.proto', {});
const grpcObject = grpc.loadPackageDefinition(packageDefinition);
const SuperCoolService = grpcObject.SuperCoolService;

function sayHello(call, callback) {
callback(null, {reply: 'Hello ' + call.request.greeting});
}

const server = new grpc.Server();
server.addService(SuperCoolService.service, {sayHello: sayHello});
server.bind('127.0.0.1:50051', grpc.ServerCredentials.createInsecure());
server.start();

Voila! Your gRPC server is ready to shake hands with clients like an overzealous politician.

Implementing the Client

Next, let’s carve out a spiffy Node.js gRPC client to interact with our eloquent server.

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');

const packageDefinition = protoLoader.loadSync('super_cool_service.proto', {});
const grpcObject = grpc.loadPackageDefinition(packageDefinition);
const SuperCoolService = grpcObject.SuperCoolService;

const client = new SuperCoolService(
'localhost:50051',
grpc.credentials.createInsecure()
);

client.sayHello({greeting: 'World'}, (error, response) => {
console.log(response.reply);
});

Executing this client script whispers sweet nothings into the server’s ear, and it responds like a lover scorned, shouting “Hello World”.

gRPC, this be the way light traverses in the realm of web protocols. Get this under your toolbelt, and you’ve hopped the grumpy wagon to becoming a more efficient developer. Sure, there could be a few dragon fights on the way, but rains of binary fire make life interesting, right?

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ahmet Soner
Ahmet Soner

Written by Ahmet Soner

Software Architect | Specializing in distributed systems and scalable architectures | Enthusiast of cutting-edge technologies and innovation

No responses yet

Write a response