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