Upstash Documentation

Server-Side Usage

1 min read

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#

limitnumber#

Maximum number of messages to retrieve (capped at 1000)

Default: "1000"

startnumber#

Fetch messages after this Unix timestamp (in milliseconds)

endnumber#

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#

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

Next Steps#