Skip to main content
Upstash Realtime is the easiest way to add realtime features to any Next.js project.
Upstash Realtime

Why Upstash Realtime?

  • โฐ Setup takes 60 seconds
  • ๐Ÿงจ Clean APIs & first-class TypeScript support
  • โšก Extremely fast, zero dependencies, 1.9kB gzipped
  • ๐Ÿ’ป Deploy anywhere: Vercel, Netlify, etc.
  • ๐Ÿ’Ž 100% type-safe using zod v4 or zod mini
  • ๐Ÿ”Œ Automatic connection management w/ message delivery guarantee
  • ๐Ÿ”‹ Built-in middleware and helpers - batteries included
  • ๐Ÿ“ถ HTTP-based: Redis streams & server-sent events

Quickstart

Get Upstash Realtime running in your Next.js app in under 60 seconds.

1. Installation

npm install @upstash/realtime

2. Configure Upstash Redis

Upstash Realtime is powered by Redis. Grab your credentials from the Upstash Console.
Add them to your environment variables:
.env
UPSTASH_REDIS_REST_URL=https://striking-osprey-20681.upstash.io
UPSTASH_REDIS_REST_TOKEN=AVDJAAIjcDEyZ...
Create a Redis instance:
lib/redis.ts
import { Redis } from "@upstash/redis"

export const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL,
  token: process.env.UPSTASH_REDIS_REST_TOKEN,
})

3. Define Event Schema

Define the structure of realtime events in your app:
lib/realtime.ts
import { Realtime, InferRealtimeEvents } from "@upstash/realtime"
import { redis } from "./redis"
import z from "zod"

const schema = {
  notification: z.object({
    alert: z.string(),
  }),
}

export const realtime = new Realtime({ schema, redis })
export type RealtimeEvents = InferRealtimeEvents<typeof realtime>

4. Create Realtime Route Handler

Create your realtime endpoint under /api/realtime/route.ts:
Itโ€™s important that your route matches this path exactly: /api/realtime/route.ts
app/api/realtime/route.ts
import { handle } from "@upstash/realtime"
import { realtime } from "@/lib/realtime"

export const GET = handle({ realtime })

5. Emit Events

From any server component or API route:
app/api/notify/route.ts
import { realtime } from "@/lib/realtime"

export const POST = async () => {
  // ๐Ÿ‘‡ 100% type-safe
  await realtime.notification.alert.emit("Hello world")
  return new Response("OK")
}

6. Subscribe to Events

In your client components:
app/components/notifications.tsx
"use client"

import { useRealtime } from "@upstash/realtime/client"
import type { RealtimeEvents } from "@/lib/realtime"

export default function Notifications() {
  useRealtime<RealtimeEvents>({
    events: {
      notification: {
        alert: (data) => console.log(data),
      },
    },
  })

  return <div>Listening for events...</div>
}
Thatโ€™s it! Your app is now listening for realtime events with full type safety. ๐ŸŽ‰

Next Steps