0Pricing
gRPC & High Performance APIs · 课时

流式 RPC:服务器、客户端与双向流

超越一元调用,学习 gRPC 的三种流式模式,了解如何通过一次调用发送消息序列。

流式 RPC:服务器、客户端与双向流 是 CoddyKit 上的免费 gRPC & High Performance APIs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 gRPC & High Performance APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 gRPC & High Performance APIs 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Beyond Unary

A unary RPC is one request, one response. But many problems need a sequence of messages — feeds, uploads, chat.

gRPC offers three streaming modes built on HTTP/2 streams.

The Four Method Shapes

In a .proto service you can declare:

  • Unary: one in, one out.
  • Server streaming: one in, many out.
  • Client streaming: many in, one out.
  • Bidirectional: many in, many out.

Declaring Streams in Proto

The stream keyword marks a parameter as a stream.

service Feed {
  rpc Watch (WatchRequest) returns (stream Event);
  rpc Upload (stream Chunk) returns (UploadResult);
  rpc Chat (stream Message) returns (stream Message);
}

Server Streaming

The client sends one request; the server replies with many messages until it closes the stream.

Great for: live feeds, large result sets, progress updates.

// Server side (Go-style pseudocode)
func (s *server) Watch(req *WatchRequest, stream Feed_WatchServer) error {
  for _, e := range events {
    stream.Send(e)
  }
  return nil
}

Client Streaming

The client sends many messages, then the server returns a single response.

Great for: file uploads, batched ingestion, aggregations.

// Server reads the whole client stream, then replies once
func (s *server) Upload(stream Feed_UploadServer) error {
  for {
    chunk, err := stream.Recv()
    if err == io.EOF {
      return stream.SendAndClose(&UploadResult{})
    }
  }
}

Bidirectional Streaming

Both sides send streams independently over the same call. Messages can interleave freely.

Great for: chat, real-time collaboration, interactive protocols.

Why It's Efficient

All four shapes ride a single HTTP/2 stream — no new connection per message.

Multiplexing means many streaming calls share one connection without head-of-line blocking.

Flow Control

HTTP/2 provides built-in flow control, so a slow reader naturally backpressures a fast writer.

This prevents a streaming server from overwhelming a client that can't keep up.

Ending a Stream

Streams close explicitly:

  • Server streaming ends when the server returns.
  • Client streaming ends when the client signals end-of-stream and the server replies.
  • Bidirectional ends when both halves complete or an error/cancel occurs.

Errors & Cancellation

Either side can cancel or fail mid-stream. The peer receives a status code and should clean up.

Always handle EOF and error returns from Recv() / Send() to avoid leaks.

Choosing a Mode

Pick by data shape:

  • One-shot request/response: unary.
  • Push many results: server streaming.
  • Send many, get a summary: client streaming.
  • Continuous two-way: bidirectional.

Quick Check

Test your understanding of streaming RPCs.

Recap

You learned gRPC's streaming modes.

  • Unary, server streaming, client streaming, and bidirectional.
  • Mark streams with the stream keyword in proto.
  • All ride a single multiplexed HTTP/2 stream with built-in flow control.
  • Choose the mode that matches your data's shape.

常见问题解答

「流式 RPC:服务器、客户端与双向流」课时是免费的吗?

是的 — 「流式 RPC:服务器、客户端与双向流」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 gRPC & High Performance APIs 课程的其余内容,请升级到 CoddyKit PRO。 gRPC & High Performance APIs 课程共包含 4 节课。

「流式 RPC:服务器、客户端与双向流」这节课中我会学到什么?

超越一元调用,学习 gRPC 的三种流式模式,了解如何通过一次调用发送消息序列。 你通过在浏览器中直接运行的动手代码来练习 gRPC & High Performance APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 gRPC & High Performance APIs 需要有经验吗?

无需任何先前经验。CoddyKit 上的 gRPC & High Performance APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「流式 RPC:服务器、客户端与双向流」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 gRPC & High Performance APIs 课中编写并运行代码吗?

能。每节 gRPC & High Performance APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Protobuf 模式定义
  2. 生成 gRPC 代码
  3. 简单的一元 gRPC 服务
  4. 流式 RPC:服务器、客户端与双向流
← 返回 gRPC & High Performance APIs