Handling Reconnection and Subscription Cleanup
Make tRPC subscriptions production-ready by handling dropped connections, automatic reconnection, and proper cleanup.
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),
});All lessons in this course
- Introduction to Real-time with tRPC
- Setting Up WebSockets for Subscriptions
- Implementing Live Data Subscriptions
- Handling Reconnection and Subscription Cleanup