Emit Events
Emit events from any server context:route.ts
route.ts
Subscribe to Events
Subscribe to events on a channel:route.ts
route.ts
Unsubscribe
Clean up subscriptions when done:route.ts
Retrieve History
Fetch past messages from a channel:route.ts
History Options
Maximum number of messages to retrieve (capped at 1000)
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
route.ts
- Fetches messages matching the history criteria
- Replays them in chronological order
- Continues to listen for new messages
Use Cases
Background Jobs
Background Jobs
Stream progress updates from long-running tasks:
app/api/job/route.ts
Event Processing
Event Processing
Process events with server-side logic:
route.ts
Multi-Channel Broadcasting
Multi-Channel Broadcasting
Emit events to multiple channels:
route.ts
Webhook Processing
Webhook Processing
Forward webhook events to realtime channels:
app/api/webhook/route.ts
Next Steps
History
Configure message retention and expiration
Channels
Scope events to specific rooms or users