Use Upstash Realtime on the server to emit events, subscribe to channels, and retrieve message history.
Emit Events#
Emit events from any server context:
route.ts
Emit to specific channels:
route.ts
Subscribe to Events#
Subscribe to events on a channel:
route.ts
Subscribe to multiple events:
route.ts
Unsubscribe#
Clean up subscriptions when done:
route.ts
Retrieve History#
Fetch past messages from a channel:
route.ts
History Options#
Fetch messages after this Unix timestamp (in milliseconds)
Fetch messages before this Unix timestamp (in milliseconds)
route.ts
History Response#
Each history message contains:
Subscribe with History#
Replay past messages and continue subscribing to new ones:
route.ts
Pass history options:
route.ts
This pattern:
- Fetches messages matching the history criteria
- Replays them in chronological order
- Continues to listen for new messages
Use Cases#
Background Jobs
Stream progress updates from long-running tasks:
app/api/job/route.ts
Event Processing
Process events with server-side logic:
route.ts
Multi-Channel Broadcasting
Emit events to multiple channels:
route.ts
Webhook Processing
Forward webhook events to realtime channels:
app/api/webhook/route.ts