A simple, real-time chat application built with Next.js 15, Supabase real-time subscriptions, and TypeScript. Experience instant messaging with live user presence indicators! ⚡
- 🔥 Real-time messaging - Messages appear instantly across all connected clients
- 👥 Live user presence - See who's online in real-time with animated indicators
- 💨 Lightning fast - Built with Next.js 15 and optimized with PNPM
- 🔒 Type-safe - Full TypeScript implementation throughout
- 🎨 Clean UI - Designed with Tailwind CSS and smooth scroll animation
Check out the live application: RealtimeChat.app
- Frontend: Next.js 15 (App Router) + React 19
- Backend: Supabase (PostgreSQL + Real-time)
- Language: TypeScript
- Styling: Tailwind CSS
- Package Manager: PNPM
- Real-time: Supabase Subscriptions (WebSocket)
├── app/
│ ├── globals.css
│ ├── layout.tsx
│ └── page.tsx
├── components/
│ └── chat-room.tsx
├── hooks/
│ └── useRealtime.ts
├── lib/
│ └── supabase/
│ └── client.ts
└── configs..
- Node.js 18+
- PNPM (recommended) or npm
- A Supabase account (free tier)
git clone https://github.com/yourusername/realtime-chat-app.git
cd realtime-chat-apppnpm install- Create a new project at supabase.com
- Go to Settings → API and copy your project URL and anon key
- In the SQL Editor, run this schema:
-- Enable real-time for public schema
ALTER PUBLICATION supabase_realtime ADD TABLE messages;
ALTER PUBLICATION supabase_realtime ADD TABLE profiles;
-- Messages table
CREATE TABLE messages (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
content TEXT NOT NULL,
user_name VARCHAR(50) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Profiles table for online status
CREATE TABLE profiles (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE,
online BOOLEAN DEFAULT false,
last_seen TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Enable Row Level Security
ALTER TABLE messages ENABLE ROW LEVEL SECURITY;
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
-- Allow all operations (adjust for production)
CREATE POLICY "Allow all operations" ON messages FOR ALL USING (true);
CREATE POLICY "Allow all operations" ON profiles FOR ALL USING (true);Create a .env.local file in the root directory:
NEXT_PUBLIC_SUPABASE_URL=your_project_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_anon_keypnpm devOpen http://localhost:3000 and start chatting! 🎉
The app uses Supabase's real-time subscriptions to listen for database changes:
// Listen for new messages
channel = supabase
.channel("messages")
.on(
"postgres_changes",
{ event: "INSERT", schema: "public", table: "messages" },
(payload) => setMessages((prev) => [...prev, payload.new as Message])
)
.subscribe();Online status is managed automatically:
- Users go "online" when they join the chat
- Status updates in real-time across all connected clients
- Automatic cleanup on tab close/refresh using
beforeunloadevent
- Connection rate limiting: Limited to 10 events per second
- Message limits: Only loads last 50 messages initially
- Automatic cleanup: Proper subscription cleanup prevents memory leaks
# Development
pnpm dev # Start development server
pnpm build # Build for production
pnpm start # Start production server
# Code Quality
pnpm lint # Run ESLint
pnpm type-check # Run TypeScript checksFor production deployment, consider:
- Row Level Security (RLS): Implement proper RLS policies
- Authentication: Add user authentication with Supabase Auth
- Rate Limiting: Implement message rate limiting
- Content Moderation: Add message filtering/moderation
- Environment Variables: Secure your environment variables
- Push your code to GitHub
- Connect your repository to Vercel
- Add your environment variables in Vercel dashboard
- Deploy! 🎉
Contributions are welcome! Here's how you can help:
- Fork the project
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
Real-time not working?
- Check if your Supabase project has real-time enabled
- Verify your environment variables are correct
- Make sure you've run the SQL schema
Messages not appearing?
- Check browser console for errors
- Verify database policies allow read/write operations
- Ensure your API keys are valid
Connection issues?
- Check your internet connection
- Verify Supabase service status
- Try refreshing the page
This project is licensed under the MIT License - see the LICENSE file for details.
If you found this project helpful, please consider giving it a star! ⭐
Built with ❤️ by Laxman Rathod
🐦 Twitter • 💼 LinkedIn • 📝 Dev.to
Want to build something similar? Check out my blog post for a detailed step-by-step guide! 🚀