Implementing Live Data Subscriptions
Create tRPC subscription procedures to push real-time updates from the server to connected clients.
Real-time Data with Subscriptions
Welcome to the final lesson on tRPC real-time! We've learned about WebSockets, now let's build live data features.
Subscriptions in tRPC allow your server to push real-time updates to connected clients. Unlike queries (which pull data) or mutations (which modify data), subscriptions are push-based.
- Queries: Client asks, server responds once.
- Mutations: Client sends data, server performs action, responds once.
- Subscriptions: Client asks once, server sends updates over time.
Defining Server Subscriptions
To create a subscription on your tRPC server, you use the createSubscription helper, similar to createQuery or createMutation.
A key difference is that a subscription procedure returns an observable. An observable is a stream of data that can emit multiple values over time.
Let's look at the basic structure.
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