Building API with gRPC using ProtoBuf – Part 3

Reading Time: 3 minutes

In the previous blog, we discussed the Protocol buffers. How do they work? How to define the protobuf and compile it? Whats happens when we compile our protobuf file? How Protocol buffers were designed to solve many problems? Also how to generate Java or Scala code? In order to continue our series, we will try to create a small basic application with simple client requests and servers response.

In order to build the client or server-side API, we need to follow some steps:

Step 1: Let’s Define the protobuf for our small application.

In first line you have to define the proto syntax i.e proto2 or proto3

syntax = "proto3";

In the next line you should define the package structure i.e when you compile the proto file the code will generate in the given package structure:

package knoldus.protos;

Now you can define your proto messages part like below:

message HelloRequest {
string name = 1;
HelloLastName last = 2;

Here is the complete proto file:


Step 2: Now simply compile you code.

It will generate all code that will help you to the client or severe side code. You can write both synchronous and asynchronous code using.


When you compiled the protobuf, classes under hello package will generate.

Step 3: Define the Client side code in Scala.

Create the HelloWorldClient class as shown below:


In the above code the blockingStub.sayHello is a server side method that will give the response according to our request.

Now create the companion object for the HelloWorldClient:


Now run the main method after server is running, you will see the following output:

It will greet Shiv

Step 4: Now let’s see how can we define the Server-side code:

So here is the example for server side code: HelloWorldServer Class

In this example we bind our service with port 50051. And also define our sayHello method

Lets also define the campanion object for HelloWorldServer like HelloWorldClient:


Now run the main method:

Your server is start now.

Now if you run above code on your machine you able to run your little client and server-side API by simply running the main method of both classes.


In this blog, we are able to create small and simple API. What is the directory structure of the API? We also saw the response of the server.

* gRPC documentation.
* scalaPB example