Implementing gRPC Server Side Streaming With Go
By: Pramono Winata
Tags: Technology, Grpc, Go, Programming, Tech

Implementation

Photo by Cam Adams on Unsplash

It’s implementation time! If you are reading this section, I assumed you already know about this 3 things:

gRPC

Server Side Streaming

Go

Server side streaming is especially useful if your server must return a bulky payload. By using streaming, you can split up those responses and return them one by one, and client will be able to cut off unused responses if the client already has sufficient response or when it has been waiting too long for some responses.

Okay now let’s jump straight in to the code!

Proto File

For starter we will need to define our protobuf file to be used by client and server side, let’s just make a simple one here.

This proto file basically contains a single function call with parameter Request and returns a stream of Response .

Before we proceed, we will also need to generate our pb file to be used by our Go Program. Each programming language will have different way to generate the protocol buffer file, for Go we will be using protoc library.

If you haven’t installed it yet, Google provided the installation guide for that.

Let’s generate the protocol buffer file by running:

protoc --go_out=plugins=grpc:. *.proto

And we will have data.pb.go ready to be used.

Client File

For the next step, you can either make the client or server file in any order.

But in this example i will be making client file first.

Client will basically be the one sending request and will be the one that will receive multiple responses.

The client will call the gRPC method FetchResponse and wait for all the responses. I am using goroutine here to show the possibility of the concurrency here and using channel in order to wait until all the processes are finished before exiting the program.

Server File

For the third and final file, we will be making the server file. This file will receive the response from client and in turn sending a stream of responses into client.

In the server file, i am using goroutine also to simulate concurrent process.

For each of the request, i will be streaming 5 requests back to the client side. Each also with different process time in order to simulate different processing time.

Output

Now comes the exciting part, let’s build both of our client and server file with go build to get our binary file and open up two separate console command to run it.

You should turn on the server first before the client since the client will directly invoke the server method.

So let’s go inside the directory of each of your binary files and run both of them with /.server and ./client .

Your client will output this:

2020/11/10 22:26:11 Resp received: Request #0 For Id:1

2020/11/10 22:26:12 Resp received: Request #1 For Id:1

2020/11/10 22:26:13 Resp received: Request #2 For Id:1

2020/11/10 22:26:14 Resp received: Request #3 For Id:1

2020/11/10 22:26:15 Resp received: Request #4 For Id:1

2020/11/10 22:26:15 finished

And the server will output this:

2020/11/10 22:26:09 start server

2020/11/10 22:26:11 fetch response for id : 1

2020/11/10 22:26:11 finishing request number : 0

2020/11/10 22:26:12 finishing request number : 1

2020/11/10 22:26:13 finishing request number : 2

2020/11/10 22:26:14 finishing request number : 3

2020/11/10 22:26:15 finishing request number : 4

If all is well, you have successfully built yourself a grpc server side streaming with Go! If you need the github code for the whole example, i do provide it here.