Overview
- Small private, self-destructing chat built with Next.js and Elysia.
- Realtime features use Elysia realtime/Redis and Upstash for persistence.
Quick Start (local)
- Install
npm install- Environment
- Copy
.envexample (or create.env.local) and set:NEXT_PUBLIC_API_URL=http://localhost:3000UPSTASH_REDIS_REST_URLandUPSTASH_REDIS_REST_TOKEN
- Run dev server
npm run devKey files
- API server (Elysia routes):
src/app/api/[[...slugs]]/route.ts - Client helper:
src/lib/client.ts(usegetClient()in browser code) - CORS config helper:
src/lib/cors.ts - Realtime helpers:
src/lib/realtime.ts,src/lib/realtime-client.ts
Environment & Deployment
- In Vercel, set the following environment variables (Production scope):
NEXT_PUBLIC_API_URL=https://<your-vercel-app>.vercel.appUPSTASH_REDIS_REST_URLandUPSTASH_REDIS_REST_TOKEN
- Redeploy after updating env vars.
CORS & Common Issues
- The deployed app must not call
localhost:3000. Ensuresrc/lib/client.tsresolves the API origin at runtime in the browser (useswindow.location.originorNEXT_PUBLIC_API_URL). - Server must expose CORS headers. Use the Elysia CORS plugin and the helper at
src/lib/cors.ts:
import { cors } from '@elysiajs/cors'
import { getCorsConfig } from '@/lib/cors'
new Elysia().use(cors(getCorsConfig())).get(...)- For cookies/credentials, set
credentials: trueand use a specific allowed origin (not*).
Troubleshooting
- Error
ERR_CONNECTION_REFUSEDtohttp://localhost:3000/api/...in production: your client was built withlocalhostbaked-in. Use a runtime client (getClient()) and setNEXT_PUBLIC_API_URLin Vercel. - CORS preflight failing: ensure your route handlers respond to
OPTIONSand that CORS plugin is applied globally or before routes.
Using the treaty client (browser)
import { getClient } from '@/lib/client'
const client = getClient()
await client.room.create.post()Next steps
- Review this file and adjust any organization-specific URLs or commands.
- Confirm
NEXT_PUBLIC_API_URLin Vercel and redeploy.
Credits
- Built with Next.js, Elysia, and Upstash (Redis).