A lightweight chat application demonstrating Supabase Realtime for instant message updates, user authentication with RLS, and presence tracking. Built with React/Next.js, this starter is designed to be easy to extend—add rooms, threads, reactions, or even turn it into a live support system.
- 🔐 Authentication - Email/password authentication with Supabase Auth
- 💬 Real-time Messaging - Instant message updates using Supabase Realtime subscriptions
- 👥 Presence Tracking - See who's online in real-time
- 🔒 Row Level Security (RLS) - Secure data access with PostgreSQL policies
- 🎨 Modern UI - Clean, responsive design with Tailwind CSS and dark mode support
- ⚡ Next.js 14 - Built with App Router and Server Components
- 📱 Responsive - Works seamlessly on desktop and mobile devices
- Node.js 18+ and npm/yarn/pnpm
- A Supabase account (sign up for free)
git clone <your-repo-url>
cd supabase-realtime-chat-templatenpm install
# or
yarn install
# or
pnpm install- Create a new project at supabase.com
- Go to your project's SQL Editor
- Run the migration file
supabase/migrations/001_initial_schema.sqlto create the necessary tables and policies
-
Copy
.env.local.exampleto.env.local:cp .env.local.example .env.local
-
Get your Supabase credentials:
- Go to your Supabase project settings
- Navigate to API settings
- Copy your Project URL and
anonpublic key
-
Update
.env.localwith your credentials:NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
npm run dev
# or
yarn dev
# or
pnpm devOpen http://localhost:3000 in your browser.
- Navigate to
/auth(or you'll be redirected automatically) - Sign up with your email and password
- Start chatting!
├── app/
│ ├── auth/
│ │ └── page.tsx # Authentication page
│ ├── globals.css # Global styles
│ ├── layout.tsx # Root layout
│ └── page.tsx # Main chat page
├── components/
│ ├── AuthForm.tsx # Sign in/sign up form
│ ├── Chat.tsx # Main chat component
│ ├── MessageList.tsx # Message display component
│ ├── MessageInput.tsx # Message input component
│ └── UserList.tsx # Online users sidebar
├── lib/
│ └── supabase/
│ ├── client.ts # Browser Supabase client
│ ├── server.ts # Server Supabase client
│ └── middleware.ts # Middleware helper
├── supabase/
│ └── migrations/
│ └── 001_initial_schema.sql # Database schema
└── middleware.ts # Next.js middleware for auth
The application uses two main tables:
- Extends Supabase Auth users
- Stores username and avatar URL
- Automatically created when a user signs up
- Stores chat messages
- Linked to users via
user_id - Includes content and timestamp
- Enabled for Realtime subscriptions
Both tables use Row Level Security (RLS) policies to ensure users can only modify their own data while allowing read access to all authenticated users.
Messages are synchronized in real-time using Supabase's Postgres Realtime feature:
- When a user sends a message, it's inserted into the
messagestable - Supabase Realtime broadcasts the change to all subscribed clients
- The
Chatcomponent listens for INSERT, UPDATE, and DELETE events - Messages are automatically added/updated/removed in the UI
User presence is tracked using Supabase Realtime Presence:
- When a user joins, they track their presence in a dedicated channel
- Other users receive presence updates (join/leave events)
- The online users list updates automatically
- Presence is cleaned up when users disconnect
- Users sign up/sign in via the
/authpage - Supabase Auth handles authentication
- A profile is automatically created via database trigger
- Middleware refreshes the session on each request
- Protected routes redirect unauthenticated users
This template is designed to be easily extended. Here are some ideas:
- Create a
roomstable - Add
room_idto messages - Filter messages by room
- Add room selection UI
- Add
parent_message_idto messages - Create nested message display
- Add reply functionality
- Create a
reactionstable - Add reaction buttons to messages
- Display reaction counts
- Use Supabase Storage
- Add file upload component
- Display images/files in messages
- Add ticket/thread management
- Add agent assignment
- Add status tracking
- Add priority levels
- Next.js 14 - React framework with App Router
- TypeScript - Type-safe JavaScript
- Supabase - Backend as a service (Auth, Database, Realtime)
- Tailwind CSS - Utility-first CSS framework
- React - UI library
MIT
Contributions are welcome! Feel free to open an issue or submit a pull request.