Streaming in gRPC
Streaming RPCs let you transfer sequences of messages instead of a single one. gRPC Fundamentals & Contracts lists the three types: server streaming, client streaming and bidirectional. This guide shows how to implement them in a Sword controller.
Server streaming
In server streaming the client sends one request and the server responds with a stream of messages. The trait generated by tonic defines an associated type per method that you assign to GrpcStream<T>, and you must return it wrapped in GrpcResult<T>.
You can build the stream with async_stream::try_stream and wrap it with GrpcResponse::stream():
use std::sync::Arc;
use async_stream::try_stream;
use sword::grpc::*;
use sword::prelude::*;
use tokio::time::{self, Duration};
#[controller(kind = Controller::Grpc, server = UserGrpcServiceServer)]
pub struct UsersController {
users: Arc<UserRepository>,
}
#[sword::grpc::async_trait]
impl UserGrpcService for UsersController {
type StreamUsersStream = GrpcStream<UserItem>;
async fn stream_users(
&self,
_: Request<StreamUsersRequest>,
) -> GrpcResult<Self::StreamUsersStream> {
let users = self.users.find_all().await;
let output = try_stream! {
for user in users {
yield UserItem::from(&user);
time::sleep(Duration::from_secs(1)).await;
}
};
Ok(GrpcResponse::stream(output))
}
}Client streaming
In client streaming the client sends a stream of messages and the server responds with a single message.
use sword::grpc::*;
use sword::prelude::*;
#[sword::grpc::async_trait]
impl UserGrpcService for UsersController {
async fn upload_users(&self, req: Request<Streaming<UserItem>>) -> GrpcResult<UploadReply> {
let mut stream = req.into_inner(); // extracts the stream to iterate over the messages
let mut count = 0u32;
while let Some(item) = stream.message().await? {
// ... process each item ...
count += 1;
}
Ok(GrpcResponse::message(UploadReply { count }))
}
}Bidirectional streaming
In bidi streaming both sides send and receive streams concurrently.
use async_stream::try_stream;
use sword::grpc::*;
use sword::prelude::*;
#[sword::grpc::async_trait]
impl UserGrpcService for UsersController {
type ChatStream = GrpcStream<ChatReply>;
async fn chat(&self, req: Request<Streaming<ChatMessage>>) -> GrpcResult<Self::ChatStream> {
let mut incoming = req.into_inner();
let output = try_stream! {
while let Some(message) = incoming.message().await? {
// ... build one response per message ...
yield ChatReply { text: message.text };
}
};
Ok(GrpcResponse::stream(output))
}
}API Reference: Streaming gRPC
The 'GrpcStream' type
pub type GrpcStream<T> = Pin<Box<dyn Stream<Item = Result<T, Status>> + Send + 'static>>;Returns
- A boxed stream of
Tmessages orStatuserrors.
When to use it
- As the type of the associated stream in server streaming methods (
type StreamUsersStream = GrpcStream<T>) and as the result ofGrpcResponse::stream().
Responding with a gRPC stream
pub fn stream<T, S>(stream: S) -> tonic::Response<GrpcStream<T>>
where
S: Stream<Item = Result<T, Status>> + Send + 'static,Returns
- A
Response<GrpcStream<T>>wrapping a stream.
When to use it
- In server and bidirectional streaming methods to return a sequence of messages.
Notes
GrpcResponseis a stateless struct: its methods are static constructors.- The associated type defined by the generated trait must match
GrpcStream<T>. Otherwise, the implementation will not compile. async_streamis an optional but convenient dependency for building streams. You can also implementStreamby hand if you prefer.

