0Pricing
tRPC End-to-End Type Safe APIs · Lesson

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

  1. Introduction to Real-time with tRPC
  2. Setting Up WebSockets for Subscriptions
  3. Implementing Live Data Subscriptions
  4. Handling Reconnection and Subscription Cleanup
← Back to tRPC End-to-End Type Safe APIs