gRPC Streaming
In gRPC, streaming is used when a simple request-response pattern (like in typical HTTP APIs) isn’t sufficient—usually due to performance, real-time data needs, or the nature of the communication. gRPC supports four types of communication:
-
Unary RPC – Single request, single response
-
Server-Side Streaming RPC – Single request, stream of responses
-
Client-Side Streaming RPC – Stream of requests, single response
-
Bidirectional Streaming RPC – Stream of requests, stream of responses
Here’s when to use each streaming type:
1. Unary RPC
Use when: You just need a simple request-response.
✅ Most common, lightweight, and easy to debug.
Example:
-
Getting a user profile by ID
-
Fetching a product detail page
rpc GetUserById(Request) returns (User);
2. Server-Side Streaming
Use when:
-
You send a single request and expect multiple results over time.
-
The client doesn’t need to send anything else after the first request.
Use cases:
-
Logs/Events stream: Get logs from a server for a specific timeframe
-
Search results pagination: Stream results as they’re found
-
Large data export: Chunked file download or list of users
rpc ListUsers(Request) returns (stream User);
3. Client-Side Streaming
Use when:
-
The client needs to send a sequence of data before getting a response.
-
Great when batching requests or uploading chunks.
Use cases:
-
Uploading files in chunks
-
Sending multiple metrics or sensor data
-
Batch create/update operations
rpc UploadMetrics(stream Metric) returns (Response);
4. Bidirectional Streaming
Use when:
-
Both sides need to send a stream of messages independently.
-
Useful for real-time, duplex communication.
Use cases:
-
Chat applications
-
Live gaming updates
-
Real-time collaboration tools (e.g., Google Docs)
-
IoT device communication with a central server
rpc Chat(stream Message) returns (stream Message);