p2pchat
A fully functional peer-to-peer chat application using WebRTC, Supabase, and GitHub Pages. This application enables direct browser-to-browser communication without any backend servers for message transmission.
- Language
- HTML
- Stars
- 0
- Last updated
From the README
Peer-to-Peer Chat Application
A fully functional peer-to-peer chat application using WebRTC, Supabase, and GitHub Pages. This application enables direct browser-to-browser communication without any backend servers for message transmission.
Features
- Real-time peer-to-peer text chat using WebRTC DataChannel
- Multi-user support with full mesh network topology
- Supabase Realtime Database for signaling
- Responsive design with dark mode toggle
- Random username generation
- Room-based chat with link sharing
- GitHub Pages deployment ready
- Automatic TURN server integration for better connectivity
Files Included
index.html- Main application interfacestyle.css- Styling for the applicationapp.js- Core application logicconfig.js- Supabase configurationturn_helper.js- TURN server helper for Cloudflare integrationREADME.md- This documentationSUPABASE_SETUP.md- Detailed Supabase setup guidesupabase_schema.sql- SQL schema for Supabase tablefix_signals_table.sql- Fix script for ID column issuessupabase_test.html- Supabase connection testing toolsignals_table.csv- CSV template for table importwebrtc_test.html- WebRTC connectivity testing toolturn_test.html- TURN server testing toolWEBRTC_TROUBLESHOOTING.md- Detailed WebRTC troubleshooting guide
Prerequisites
Setup Instructions
1. Supabase Setup
Create a Supabase Project
- Go to Supabase and create a new project
- Note down your Project URL and anon key from
Settings > API
Create the Signals Table
You can either:
- Use the
supabase_schema.sqlfile and run it in the Supabase SQL editor, or - Follow the manual steps in
SUPABASE_SETUP.md
Important: Make sure the id column is properly configured as an auto-incrementing primary key. See the "Fixing ID Column Issues" section in SUPABASE_SETUP.md if you encounter errors.
Configure Row Level Security (RLS)
Policy 1: Enable read access for all
- Name:
Enable read access for all - Operation:
SELECT - Using expression:
true
Policy 2: Enable insert access for all
- Name:
Enable insert access for all - Operation:
INSERT - With check expression:
true
These policies allow all users to read and insert signaling data, which is necessary for the WebRTC peer discovery and connection process. The room_id field ensures users only see signals from their room.
2. Configure the Application
- Open
config.jsin your code editor - Replace the placeholder values with your Supabase credentials:
const SUPABASE_URL = 'https://your-project.supabase.co'; const SUPABASE_ANON_KEY = 'your-anon-key-here';
3. Test Locally
- Open
index.htmlin your browser - Create a room by entering a room ID and clicking "Join Room"
- Copy the room link and open it in another browser or private/incognito window
- Test sending messages between browsers
Note: For testing with multiple users on the same machine, use different browsers or private/incognito windows to ensure proper WebRTC functionality.
You can also use supabase_test.html to verify your Supabase connection is working properly.
Deployment to GitHub Pages
Method 1: GitHub Repository
- Create a new GitHub repository
- Push your code to the repository:
git init git add . git commit -m "Initial commit" git branch -M main git remote add origin https://github.com/your-username/your-repo-name.git git push -u origin main
- Go to your repository settings on GitHub
- Scroll down to "Pages" section
- Under "Source", select "Deploy from a branch"
- Select "main" branch and "/ (root)" folder
- Click "Save"
- Your app will be available at
https://your-username.github.io/your-repo-name/
Method 2: GitHub Pages from Folder
If you have GitHub Desktop installed:
- Create a new repository using GitHub Desktop
- Drag all files into the repository folder
- Commit and push the changes
- Follow steps 3-8 from Method 1
How It Works
-
Signaling: When a user joins a room, the app uses Supabase Realtime to broadcast their presence. Other users in the room receive this signal and initiate WebRTC connections.
-
Connection: Each user creates a direct peer-to-peer connection with every other user in the room using WebRTC DataChannel, forming a full mesh network.
-
Messaging: All messages are sent directly between browsers through the DataChannel, with no server involvement after the initial connection.
-
Multi-user Support: The app supports multiple users in the same room by establishing a full mesh network where each user connects directly to every other user.
-
TURN Server Integration: The application automatically fetches TURN server credentials from Cloudflare's free service to improve connectivity through restrictive networks.
Security Notes
- The application uses Supabase's anon key which is safe to expose in client-side code
- Row Level Security ensures users can only access data from their room
- No sensitive information is stored in the database
- All communication after connection establishment is peer-to-peer
- TURN credentials are fetched securely from Cloudflare
Troubleshooting
Common Issues and Solutions
-
No data appears in Supabase table:
- Check browser console for JavaScript errors
- Verify Supabase credentials in
config.jsare correct - Confirm the
signalstable exists with the correct schema - Ensure RLS policies are properly configured
- Check Supabase dashboard for any error messages
-
Connection Issues:
- Ensure you're using a supported browser (Chrome, Firefox, Edge)
- Check that both users are using the exact same room ID
- Make sure both users joined the room (the first user must be in the room when the second user joins)
- Some networks (especially corporate networks) may block WebRTC connections
-
Messages Not Sending:
- Check browser console for errors
- Ensure WebRTC is not blocked by browser extensions or firewall
- Verify that the WebRTC connection is established (check connection status)
- Confirm that data channels are opening properly
-
Supabase Errors:
- Double-check your Project URL and anon key
- Verify RLS policies are correctly configured
- Ensure the
signalstable has the correct schema - Check that you're using the anon key and not the service key
- "null value in column 'id' violates not-null constraint": The ID column is not configured for auto-increment (see SUPABASE_SETUP.md)
-
WebRTC Connection Issues:
- Peers can't connect to each other
- Messages aren't being sent between peers
- ICE candidates aren't being generated
- Data channels aren't opening
- STUN servers are unreachable
For WebRTC-specific issues, see WEBRTC_TROUBLESHOOTING.md for detailed troubleshooting steps.
Debugging Steps
- Open the browser's developer tools (F12)
- Check the Console tab for any error messages
- Check the Network tab to see if requests to Supabase are successful
- Verify that the Supabase client is being initialized correctly
- Check that the Realtime subscription is working
- Look for any WebRTC-related errors
Testing the Supabase Connection
- Make sure you can access your Supabase project URL in the browser
- Verify that your anon key is correct by checking it in the Supabase dashboard
- Try manually inserting a record into the signals table through the Supabase SQL editor:
INSERT INTO signals (room_id, sender, sender_name, type) VALUES ('test-room', 'test-user', 'Test User', 'test');
You can also use the supabase_test.html file included in the project to test your Supabase connection.
Testing WebRTC Connectivity
Use the webrtc_test.html file to test:
- Browser WebRTC support
- STUN server connectivity
- ICE candidate generation
- Data channel functionality
Testing TURN Server Connectivity
Use the turn_test.html file to test:
- TURN server credential fetching
- TURN server connectivity
- ICE candidate analysis with TURN servers
See WEBRTC_TROUBLESHOOTING.md for detailed WebRTC troubleshooting steps.
Browser Support
This application works on modern browsers that support WebRTC:
- Chrome 28+
- Firefox 22+
- Safari 11+
- Edge 15+
Note: For the best experience, use the latest versions of these browsers. Some older versions may have compatibility issues with WebRTC features.
Limitations
- NAT Traversal: Some network configurations may prevent direct peer connections
- Mobile Networks: Some mobile carriers restrict P2P connections
- Scalability: Full mesh network becomes inefficient with many users (>5)
- Connection Persistence: Users must remain connected to maintain the chat session
- TURN Server Availability: Cloudflare's TURN service may have rate limits or availability issues
Customization
You can customize the application by modifying:
style.cssfor visual appearanceapp.jsfor functionality changesindex.htmlfor UI structure
License
This project is open source and available under the MIT License.