การออกแบบแบ็กเอนด์แชตแบบเรียลไทม์ด้วย gRPC
ศึกษากรณีศึกษาจากการใช้งานจริง ตั้งแต่การออกแบบสถาปัตยกรรมแบ็กเอนด์แชตแบบเรียลไทม์ที่ขยายระบบได้ ไปจนถึงการใช้สตรีมสองทิศทางของ gRPC การกระจายข้อความ และสถานะการออนไลน์
การออกแบบแบ็กเอนด์แชตแบบเรียลไทม์ด้วย gRPC เป็นบทเรียน gRPC & High Performance APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน gRPC & High Performance APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Case Study
We design a chat backend that must deliver messages instantly to thousands of connected users. gRPC bidirectional streaming is a natural fit for this long-lived, two-way flow.
Defining the Service
The core is a bidi stream: clients send messages and receive others' messages over the same call.
service Chat {
rpc Connect(stream ClientEvent) returns (stream ServerEvent);
}Per-Connection Sessions
Each open stream is a user session. The server tracks the stream handle so it can push messages to that specific user later.
The Fan-Out Problem
When one user posts to a room, the server must deliver it to every other member's stream. This fan-out is the heart of chat scaling.
In-Process Hub
A simple design keeps a hub mapping rooms to subscriber channels. Posting iterates members and sends to each.
func (h *Hub) Broadcast(room string, msg *ServerEvent) {
for _, sub := range h.rooms[room] {
sub.outbox <- msg
}
}Scaling Past One Instance
Users on different server instances must still see each other. Introduce a message bus (Redis Pub/Sub, NATS, Kafka) so instances relay messages to one another.
Cross-Instance Flow
An instance publishes new messages to the bus; every instance subscribes and forwards to its own local connections. This decouples delivery from where users connect.
Presence and Typing
Presence (online/offline) and typing indicators are just more event types on the stream. Track last-seen timestamps and broadcast presence changes via the same fan-out path.
Backpressure per Client
A slow client must not stall the hub. Give each session a bounded outbox; if it overflows, drop or disconnect that client instead of blocking everyone.
select {
case sub.outbox <- msg:
default:
disconnect(sub) // slow consumer
}Reconnection and Delivery
Networks drop. Clients reconnect and resume from a last-seen message id. Persist recent history so missed messages can be replayed on reconnect.
Operational Concerns
Production chat needs:
- Keepalive to detect dead connections
- Authentication on connect (token in metadata)
- Metrics on active streams and fan-out latency
- Graceful drain on deploy
Quick Check
Test your chat-design knowledge.
Recap
You designed a real-time chat backend:
- Bidirectional streaming models each user session
- A hub fans out room messages to subscribers
- A message bus relays across multiple instances
- Presence/typing are extra event types; per-client backpressure protects the hub
- Reconnection replays missed messages; ops needs keepalive, auth, metrics, drain
คำถามที่พบบ่อย
บทเรียน “การออกแบบแบ็กเอนด์แชตแบบเรียลไทม์ด้วย gRPC” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การออกแบบแบ็กเอนด์แชตแบบเรียลไทม์ด้วย gRPC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส gRPC & High Performance APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบแบ็กเอนด์แชตแบบเรียลไทม์ด้วย gRPC”
ศึกษากรณีศึกษาจากการใช้งานจริง ตั้งแต่การออกแบบสถาปัตยกรรมแบ็กเอนด์แชตแบบเรียลไทม์ที่ขยายระบบได้ ไปจนถึงการใช้สตรีมสองทิศทางของ gRPC การกระจายข้อความ และสถานะการออนไลน์ คุณปฏิบัติ gRPC & High Performance APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน gRPC & High Performance APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน gRPC & High Performance APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การออกแบบแบ็กเอนด์แชตแบบเรียลไทม์ด้วย gRPC” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน gRPC & High Performance APIs นี้ได้ไหม
ได้ บทเรียน gRPC & High Performance APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างเกตเวย์อัตราการประมวลผลสูง
- รูปแบบความทนทานขั้นสูง
- อนาคตของ API ประสิทธิภาพสูง
- การออกแบบแบ็กเอนด์แชตแบบเรียลไทม์ด้วย gRPC