Saturday, July 12, 2025

Building bidirectional Streaming gRPC : A Real-World chat Example

A real-world example of bidirectional streaming using gRPC in a Spring Boot application with a REST wrapper for client interaction.

🔧 Technologies Used

  • Java 11
  • Spring Boot 2.7.x
  • gRPC (1.54.0)
  • Protocol Buffers (proto3)
  • Maven
  • WebFlux + Server-Sent Events (SSE) for REST integration

Code Repository Link 

Link - grpc-bidirectional-chat-server-client

1. Start gRPC Server

  • cd grpc-bidirectional-chat mvn clean install mvn spring-boot:run

2. Start REST Clien

  • cd grpc-bidirectional-chat-client mvn clean install mvn spring-boot:run

3. Upload a File via REST

  • curl -X POST "http://localhost:8081/chat?sender=Alice&message=Hello"

Project Structure

Server: grpc-bidirectional-chat


  • grpc-bidirectional-chat/ ├── pom.xml ├── src │ ├── main │ │ ├── java/com/example/grpc/ │ │ │ ├── ChatServiceImpl.java # gRPC service impl │ │ │ └── GrpcServerApplication.java # Spring Boot entry │ │ └── resources/ │ │ └── application.yml │ └── proto/ │ └── chat.proto # gRPC contract

Client: grpc-bidirectional-chat-client


  • grpc-bidirectional-chat-client/ ├── pom.xml ├── src │ ├── main │ │ ├── java/com/example/grpc/ │ │ │ ├── ChatController.java # REST controller │ │ │ └── GrpcClientApplication.java # Spring Boot entry │ │ └── resources/ │ │ └── application.yml

🧠 Class Responsibilities


🟢 Server

  • GrpcServerApplication.java
    → Main Spring Boot entry point for the gRPC server.

  • ChatServiceImpl.java
    → Implements the gRPC ChatService and handles bidirectional chat streams.

🔵 Client

  • GrpcClientApplication.java
    → Main Spring Boot entry for the client REST API wrapper.

  • ChatController.java
    → Exposes a /chat REST endpoint to initiate gRPC bidirectional streaming using stub.

🧬 Proto File 


  • syntax = "proto3"; option java_multiple_files = true; option java_package = "com.example.grpc"; option java_outer_classname = "ChatProto"; service ChatService { rpc chatStream (stream ChatMessage) returns (stream ChatMessage); } message ChatMessage { string sender = 1; string message = 2; int64 timestamp = 3; }

🔁 Diagram: REST → gRPC Flowe


  • [POST /chat?sender=Alice&message=Hello] | v +-------------------+ | Spring Controller | +-------------------+ | v +------------------------+ | gRPC Async Stub (Client) | +------------------------+ | v +------------------------+ | gRPC Server Impl | | - ChatServiceImpl | +------------------------+ | v [Responds back via StreamObserver]

When to Use gRPC bidirectional Streaming

Anti-Patterns / When Not to Use It

⚖️ gRPC Bidirectional vs Other Streaming Protocols

🚀 ALL Spring boot gRPc Post

Spring Boot gRPc All Post


You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS