การเพิ่ม Realtime Features ให้ Next.js น่าปวดหัว 💀
หมายถึงสิ่งอย่าง:
- Realtime streams สำหรับ Vercel AI SDK
- Live dashboards
- Live notifications และ updates
- Live cursors หรือ indicators 'who is online'
- Progress tracking สำหรับ file uploads หรือ long-running tasks
และนี่ definitely not เพราะขาด SaaS ที่ใช้ได้เพื่อ purpose นี้ Existing services คือ:
- Expensive & มี unclear pricing (Ably)
- Not type-safe DX ไม่ดี (Pusher)
- Awesome (Convex, Supabase) แตยากเพิ่ม realtime โดยไม่ migrate entire stack
The Goal for Upstash Realtime (เป้าหมายของ Upstash Realtime)
ผมอยาก realtime service ที่
- 100% type-safe ใน front- และ backend
- Deployable ไป Vercel และ serverless platforms อื่นๆ
- Priced based on events ไม่ใช่ connection time
- Usable กับ existing database ใดๆ (Neon, Planetscale, ทุกอย่าง)
ดังนั้นเรากำลังสร้าง Upstash Realtime ให้เป็นแค่นั้น วิธีที่ง่ายที่สุดเพื่อเพิ่ม realtime features ให้ Next.js app ใดๆ Setup ใช้ 2 นาที และ deployable ไป Vercel, Cloudflare หรืออื่นๆ
ผมเกลียด existing ways ทั้งหมด 🤡
ผมได้ไอเดีย Realtime จากการสร้าง open-source SaaS ที่ชื่อ Contentport ตอนคุณโพสต์ tweet ผ่าน contentport เรา run posting logic ใน Upstash Workflow ที่ auto-retry on failure
แบบนี้ แม้ Twitter API ลง, AI provider outage หรือ อะไรก็ตาม ผิดพลาด เรา guarantee extremely high reliability ให้คนโพสต์
แตเพราะ logic นี้รันใน background job ไม่มี connection ระหว่าง client และ posting logic กล่าวคือ ไม่มีทางบอก client เกี่ยวกับ current status (เช่น 'processing', 'posting', หรือ 'success')

เพื่อส่ง status updates ไป client ใน real-time ผมต้องการ message broker บางแบบ
ผม very unhappy กับ Pusher และไม่อยาก migrate Neon Postgres database ที่มีอยู่ไป Convex แค่เพื่อ realtime features และได้ออกมาว่า mature product สุดของเรา Upstash Redis มีทุกอย่างที่ผมต้องการ: Pub/sub และ Redis Streams
Quick Example (ตัวอย่างเร็ว)
มาสร้างตัวอย่างง่ายที่ส่ง realtime updates จาก server action ไป client
ก่อนอื่น เราติดตั้ง package:
npm install @upstash/realtime
จากนั้น define events ด้วย zod:
lib/realtime.ts
import { InferRealtimeEvents, Realtime } from "@upstash/realtime"; import z from "zod/v4"; import { redis } from "./redis"; const schema = { notification: z.object({ message: z.string(), }), }; const realtime = new Realtime({ schema, redis }); export type RealtimeEvents = InferRealtimeEvents<typeof realtime>;
ส่ง events ใน route handler ใดๆ:
route.ts
import { realtime } from "@/lib/realtime"; await realtime.emit("notification", { message: "Hello world!" });
Subscribe events ใน React component:
page.tsx
import { RealtimeEvents } from "@/lib/realtime"; import { useRealtime } from "@upstash/realtime/client"; useRealtime<RealtimeEvents>({ event: "notification", onData: ({ message }) => { console.log(message); }, });
แค่นี้! เราตอนนี้มี fully type-safe realtime updates จาก server ไป client
Disclaimer (ข้อจำกัดความรับผิดชอบ)
Upstash Realtime ไม่ได้ meant เป็น 1:1 Pusher replacement เพราะเราใช้ HTTP ไม่ใช่ sockets เหมือน Pusher
ดังนั้น แม้ HTTP จะน่าเชื่อถือกว่า สำหรับ extremely high frequency updates (>15-20 ต่อวินาที) ผมแนะนำ Pusher เพราะ socket connection ของพวกเขา
แตสำหรับอะไรก็ตามเหลือ เช่น high-frequency AI chunk updates จาก Vercel AI SDK, live dashboards หรือ live chat messages เรากำลังสร้าง Upstash Realtime ให้เป็น go-to
คุณควรลอง มัน super easy เริ่มต้น!
Thanks for reading! 🙌 มีคำถามหรือ feedback? ติดต่อ @joshtriedcoding โดยตรง