This lesson is locked. Login or Subscribe for more access!

bi-direction streaming in gRPC

Duration: 14 mins

Learn how to implement bi-directional streaming in gRPC using Go.

Instructor

Chris Shepherd

Share with a friend!

Transcript

Hey folks, so in this video, we're going to look at bi-directional streaming. These are RPCs that are a mix of both of the previous two that we've been looking at. They are RPCs that have both the client stream and the server stream, and they work independently of each other. It's really up to the implementer whether they completely send messages independently or if the client sends a message and the server waits and responds.

For this video, what we're going to implement is just a simple echo server. The client will stream messages to the server, and the server will echo those messages back to the client. To do this, we're going to create an echo RPC. This RPC will send an echo request and return an echo response for each message.

First, let's define our Protocol Buffers contract. We'll create a `.proto` file that defines the Echo service and the messages it uses. This file will have an Echo RPC that takes an `EchoRequest` and streams back an `EchoResponse`. After defining this, we'll compile it into Go code using the Protocol Buffers compiler.

Next, we'll implement the server. In our Go code, we'll create a struct that implements the Echo service interface generated from the `.proto` file. We'll handle the `Echo` method to receive a stream of requests from the client and send a stream of responses back. The server will simply echo back the message it receives, demonstrating how bi-directional streaming works.

Finally, on the client side, we'll use the generated client code to connect to the server. We'll send a stream of messages and handle the stream of responses from the server. This example shows how powerful and flexible gRPC is when working with streaming RPCs.