tRPC End-to-End Type Safe APIs · บทเรียน

การจัดการการเชื่อมต่อใหม่และการล้างการสมัครรับข้อมูล

ทำให้การสมัครรับข้อมูลของ tRPC พร้อมใช้งานจริงด้วยการจัดการการเชื่อมต่อที่หลุด การเชื่อมต่อใหม่อัตโนมัติ และการล้างทรัพยากรอย่างถูกต้อง

บทเรียน 4 จาก 413 ขั้นตอน

การจัดการการเชื่อมต่อใหม่และการล้างการสมัครรับข้อมูล เป็นบทเรียน tRPC End-to-End Type Safe APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน tRPC End-to-End Type Safe APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Real-time Is Unreliable

You can set up subscriptions, but networks drop, servers restart, and tabs sleep. Production real-time code must handle disconnects and cleanup gracefully.

The wsLink and Reconnection

tRPC clients use a WebSocket client that can automatically reconnect when a connection is lost.

import { createWSClient, wsLink } from "@trpc/client";

const wsClient = createWSClient({
  url: "ws://localhost:3000",
  retryDelayMs: (attempt) => Math.min(1000 * attempt, 5000),
});

Backoff Strategy

Reconnect attempts should use exponential backoff so a downed server is not hammered by every client at once.

Subscription Lifecycle

A subscription exposes lifecycle callbacks: data, error, started, and stopped.

trpc.onMessage.subscribe(undefined, {
  onData: (msg) => add(msg),
  onError: (err) => console.error(err),
  onStarted: () => console.log("live"),
});

Cleaning Up on Unmount

Every subscription returns an unsubscribe handle. Call it when the component unmounts to free resources.

const sub = trpc.onMessage.subscribe(undefined, { onData });
// later
sub.unsubscribe();

React Cleanup Pattern

In React, return the cleanup from useEffect so it runs on unmount.

useEffect(() => {
  const sub = trpc.onMessage.subscribe(undefined, { onData });
  return () => sub.unsubscribe();
}, []);

Server-Side Cleanup

On the server, the subscription generator must release resources when the client disconnects.

onMessage: publicProcedure.subscription(async function* (opts) {
  const queue = subscribe();
  try {
    for await (const msg of queue) yield msg;
  } finally {
    queue.close(); // cleanup on disconnect
  }
})

Detecting Missed Events

After a reconnect, the client may have missed events. Track a last-seen id and refetch the gap on reconnect.

onStarted: () => {
  fetchMissedSince(lastSeenId);
}

Heartbeats and Timeouts

Heartbeat pings detect a silently dead connection so the client can trigger a reconnect instead of waiting forever.

Avoiding Memory Leaks

Forgetting to unsubscribe leaks listeners and slowly degrades both client and server. Always pair every subscribe with an unsubscribe.

Showing Connection Status

Surface the connection state in the UI so users know when data is live versus reconnecting, improving trust in real-time views.

wsClient.connectionState; // "connecting" | "open" | "closed"

Quick Check

Test your subscription reliability knowledge.

Recap

You made subscriptions production-ready:

  • Use the WS client with exponential backoff reconnection
  • Always unsubscribe on unmount to avoid leaks
  • Clean up server-side generators and refetch missed events

Robust reconnection and cleanup keep real-time apps reliable in the real world.

เริ่มต้นได้ฟรี

เรียนรู้ tRPC End-to-End Type Safe APIs ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
10
บทเรียน
40

คำถามที่พบบ่อย

บทเรียน “การจัดการการเชื่อมต่อใหม่และการล้างการสมัครรับข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการการเชื่อมต่อใหม่และการล้างการสมัครรับข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส tRPC End-to-End Type Safe APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการการเชื่อมต่อใหม่และการล้างการสมัครรับข้อมูล”

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

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน tRPC End-to-End Type Safe APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน tRPC End-to-End Type Safe APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการการเชื่อมต่อใหม่และการล้างการสมัครรับข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน tRPC End-to-End Type Safe APIs นี้ได้ไหม

ได้ บทเรียน tRPC End-to-End Type Safe APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รู้จักการทำงานแบบเรียลไทม์ด้วย tRPC
  2. การตั้งค่า WebSockets สำหรับการสมัครรับข้อมูล
  3. การใช้งานการสมัครรับข้อมูลแบบสด
  4. การจัดการการเชื่อมต่อใหม่และการล้างการสมัครรับข้อมูล
← กลับไปที่ tRPC End-to-End Type Safe APIs