0Pricing
gRPC & High Performance APIs · บทเรียน

RPC แบบสตรีม: เซิร์ฟเวอร์ ไคลเอนต์ และสองทิศทาง

ก้าวไปไกลกว่าการเรียกแบบเอกพจน์ และเรียนรู้โหมดสตรีมทั้งสามแบบของ gRPC สำหรับส่งลำดับข้อความผ่านการเรียกครั้งเดียว

RPC แบบสตรีม: เซิร์ฟเวอร์ ไคลเอนต์ และสองทิศทาง เป็นบทเรียน gRPC & High Performance APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส gRPC & High Performance APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “RPC แบบสตรีม: เซิร์ฟเวอร์ ไคลเอนต์ และสองทิศทาง”

ก้าวไปไกลกว่าการเรียกแบบเอกพจน์ และเรียนรู้โหมดสตรีมทั้งสามแบบของ gRPC สำหรับส่งลำดับข้อความผ่านการเรียกครั้งเดียว คุณปฏิบัติ gRPC & High Performance APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน gRPC & High Performance APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน gRPC & High Performance APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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