Skip to main content
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

limit
number
default:"1000"
Maximum number of messages to retrieve (capped at 1000)
start
number
Fetch messages after this Unix timestamp (in milliseconds)
end
number
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:
  1. Fetches messages matching the history criteria
  2. Replays them in chronological order
  3. Continues to listen for new messages

Use Cases

Stream progress updates from long-running tasks:
app/api/job/route.ts
Process events with server-side logic:
route.ts
Emit events to multiple channels:
route.ts
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