A minimal anonymous chat app showcasing Supabase Realtime features:
- Broadcast - Real-time message delivery
- Presence - Online user indicators
- Database - Message persistence
- Anonymous users with random usernames (stored in localStorage)
- 4 chat rooms: General, Random, Tech, Off-Topic
- Real-time messages
- Online users per room
- Message history on join
Create a new project at database.new
Go to the SQL Editor in your Supabase dashboard and run the migration you find in supabase/migrations/001_create_messages_table.sql.
Copy .env.example to .env and fill in your Supabase credentials:
cp .env.example .envUpdate the values:
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=your-publishable-keyFind these in your Supabase project settings.
npm installnpm run devOpen http://localhost:3000 to start chatting.
Messages are instantly delivered to all connected clients via Supabase Realtime Broadcast:
channel.send({
type: 'broadcast',
event: 'message',
payload: message,
})Online users are tracked using Supabase Realtime Presence:
channel.on('presence', { event: 'sync' }, () => {
const users = Object.values(channel.presenceState()).flat()
setOnlineUsers(users)
})Messages are also stored in the database so users see history when joining:
const { data } = await supabase
.from('messages')
.select('*')
.eq('room', room)
.order('created_at', { ascending: true })
.limit(50)You can run Supabase locally using the Supabase CLI and Docker.
- Docker installed and running
- Supabase CLI (installed or used through
npx)
npx supabase startThis will start all Supabase services locally. Once started, you'll see output with your local credentials:
╭──────────────────────────────────────╮
│ 🔧 Development Tools │
├─────────┬────────────────────────────┤
│ Studio │ http://127.0.0.1:54323 │
│ ... │ ... │
╰─────────┴────────────────────────────╯
╭──────────────────────────────────────────────╮
│ 🌐 APIs │
├────────────────┬─────────────────────────────┤
│ Project URL │ http://127.0.0.1:54321 │
│ ... │ ... │
╰────────────────┴─────────────────────────────╯
...
╭──────────────────────────────────────────────────╮
│ 🔑 Authentication Keys │
├─────────────┬────────────────────────────────────┤
│ Publishable │ sb_publishable_... │
│ Secret │ sb_secret_... │
╰─────────────┴────────────────────────────────────╯
...
Copy .env.example to .env.local:
cp .env.example .env.localUpdate the values:
NEXT_PUBLIC_SUPABASE_URL=http://127.0.0.1:54321
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=sb_publishable_....Apply the database migration:
npx supabase db resetThis will apply all migrations from supabase/migrations/.
Open http://127.0.0.1:54323 to access Supabase Studio locally. Here you can:
- View and edit data in the Table Editor
- Run SQL queries
- Monitor Realtime connections
- Check logs
npx supabase stop