Skip to content

A Go microservice demo showcasing gRPC client-side streaming for calculating the average of streamed numbers. ๐Ÿ’ป Sources and related content

Notifications You must be signed in to change notification settings

sandeepkv93/golang-grpc-client-streaming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿงฎ gRPC Client-Side Streaming Demo in Go! ๐Ÿš€

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. ๐Ÿ”ข

โœจ Functionality โœจ

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. ๐Ÿ–จ๏ธ

๐Ÿ“‚ Project Structure ๐Ÿ“‚

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. ๐Ÿ“ฆ

๐Ÿš€ Running the Example ๐Ÿš€

  1. Generate gRPC code:
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/average.proto
  1. Run the server:
go run server/server.go
  1. Run the client (in a separate terminal):
go run client/client.go

๐Ÿ“ฆ Dependencies ๐Ÿ“ฆ

  • google.golang.org/grpc

๐Ÿ”‘ Key Concepts ๐Ÿ”‘

  • 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]

๐Ÿ’ก Use Cases ๐Ÿ’ก

Client-side streaming is ideal for applications that require:

  • Uploading large files or data streams. ๐Ÿ“ค
  • Sending real-time sensor data. ๐ŸŒก๏ธ
  • Processing continuous data streams. ใ€ฐ๏ธ

๐Ÿ“ Notes ๐Ÿ“

  • 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. ๐Ÿงฎ

About

A Go microservice demo showcasing gRPC client-side streaming for calculating the average of streamed numbers. ๐Ÿ’ป Sources and related content

Topics

Resources

Stars

Watchers

Forks

Languages