Unary Streaming via gRPC

Reading Time: 3 minutes

If you want to get multiple responses and send multiple requests, it is the time to use gRPC Streaming concepts. You cant do streaming with REST API as REST API uses HTTP 1.1. In this blog, I will go through how you can do Unary Streaming via gRPC.

Types of APIs or Streaming in gRPC

gRPC supports four types of APIs to support streaming.

1. Unary API
2. Server API
3. Client API
4. Bi-directional API

Unary API

Unary RPCs where the client sends a single request to the server and gets a single response back, just like a normal function call.

service GreetService {
    // Unary
    rpc Greet(GreetRequest) returns (GreetResponse) {};

When we compile this .proto file, the code will be generated for us and we will have to provide implementations on the client-side and the server-side for the generated API.

Now, first of all, we have to set-up server like this as shown in the pic

Now we have to set the medium between server and client.

Here is the “50051 is port number in which the server is running and usePlaintest() for not providing any type of security.

Now we have set up the server and channel. Now we will provide implementations on the server-side and client-side. You will use stub created from the generated code to call the generated API. Here is the server-side implementation

The request to the server is sent through the managed channel using the synchronous stub. Asynchronous stub is used in case client streaming and bidirectional streaming. Here is the client-side implementation

At the last, You have to up the server by running the server code and the send the request from the client. You will get the desired response.
Here is the source code https://github.com/Munandermaan/Streaming-via-grpc.

I will show how to do client, server and bi-directional streaming in my upcoming blogs.


After going through this blog, you will understand how to do Unary Streaming via gRPC in the easiest way.



Written by 

Munander is a Software Consultant in Knoldus Software LLP. He has done b.tech from IMS Engineering college, Ghaziabad. He has decent knowledge of C,C++,Java,Angular and Lagom. He always tries to explore new technologies. His hobbies include playing cricket and adventure.