This repository demonstrates a simple gRPC client-side streaming example in Go. ๐ป It includes two microservices: a server that calculates the average of streamed numbers and a client that sends a stream of numbers to the server. ๐ข
This project showcases the power of gRPC client-side streaming, where the client sends a stream of messages to the server, and the server responds with a single message after the client has finished streaming. ๐ This is particularly useful for scenarios like uploading large files, sending real-time sensor data, or processing continuous data streams. ๐ค
- Server: ๐ฅ๏ธ
- Implements a gRPC server that listens on port 50051. ๐ง
- Exposes a
ComputeAverage
RPC that uses client-side streaming. ๐ข - Receives a stream of numbers from the client. ๐ฅ
- Calculates the average of all received numbers. ๐งฎ
- Sends the calculated average back to the client. ๐ค
- Client: ๐ป
- Establishes a gRPC connection to the server. ๐ค
- Sends a stream of numbers (from 1 to 10) to the server every second. ๐ค
- Receives the average of the numbers from the server. ๐ฅ
- Prints the result to the console. ๐จ๏ธ
grpc-client-streaming/
โโโ proto/
โ โโโ average.proto
โโโ server/
โ โโโ server.go
โโโ client/
โ โโโ client.go
โโโ go.mod
proto/
: Contains the Protocol Buffer definition (average.proto
) for the service. ๐server/
: Contains the Go code for the gRPC server (server.go
). โ๏ธclient/
: Contains the Go code for the gRPC client (client.go
). ๐ปgo.mod
: Go module definition file. ๐ฆ
- Generate gRPC code:
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/average.proto
- Run the server:
go run server/server.go
- Run the client (in a separate terminal):
go run client/client.go
google.golang.org/grpc
- gRPC: A high-performance, open-source universal RPC framework. [cite: https]
- Protocol Buffers: A language-neutral mechanism for serializing structured data. [cite: https]
- Client-Side Streaming: A gRPC communication model where the client sends a stream of messages to the server, and the server responds with a single message after the client has finished streaming. [cite: https]
Client-side streaming is ideal for applications that require:
- Uploading large files or data streams. ๐ค
- Sending real-time sensor data. ๐ก๏ธ
- Processing continuous data streams. ใฐ๏ธ
- This example uses an insecure connection for simplicity. In a production environment, you should use secure connections with TLS. ๐
- The client sends numbers from 1 to 10, but you can modify this to send a different range of numbers or even read data from a file or another source. ๐ข
- The server calculates the average of the numbers, but you can modify this to perform any other calculation or processing on the streamed data. ๐งฎ