Overview
GenosDB’s P2P networking is powered by GenosRTC, which uses WebRTC for direct peer connections and Nostr for peer discovery and signaling. This guide covers configuring relays, TURN servers, and the cellular mesh topology for large-scale applications.Enable P2P Networking
The simplest way to enable P2P is with default settings:The database name (
'my-app') serves as the room ID. All peers connecting to the same room name will discover each other.Custom Nostr Relays
Nostr relays facilitate peer discovery and WebRTC signaling. You can specify custom relays:Finding Nostr Relays
Use nostr.watch/relays/find to discover:- Public free relays
- Paid premium relays
- Private relays
- Geographic-specific relays
Relay Selection Criteria
Latency
Choose relays geographically close to your users for faster peer discovery.
Uptime
Select relays with high availability (>99% uptime).
Capacity
Ensure relays can handle your expected peer count.
Privacy
Consider private relays for sensitive applications.
TURN Server Configuration
TURN servers help peers connect when they’re behind restrictive firewalls or NAT (Network Address Translation). They relay traffic when direct P2P connections fail.When You Need TURN
TURN is required when:- Peers are behind symmetric NAT
- Corporate firewalls block WebRTC
- Mobile networks use carrier-grade NAT
Setting Up TURN
Free TURN Providers
For testing and small projects:- Twilio (free tier with signup)
- Metered.ca (limited free tier)
- Open Relay Project (community-run)
Self-Hosted TURN
For production, consider hosting your own TURN server using coturn:Combining Relays and TURN
Cellular Mesh Configuration
For applications with 100+ concurrent peers, the cellular mesh topology reduces connection overhead from O(N²) to O(N).Standard Mesh vs. Cellular Mesh
Standard Full Mesh
Best for: < 50 peersConnections per peer: N-1 (everyone connects to everyone)Latency: Single hop (lowest)Complexity: O(N²) total connections
Cellular Mesh
Best for: 100+ peersConnections per peer: ~cellSize + bridgesLatency: Multi-hop (slightly higher)Complexity: O(N) total connections
Enabling Cellular Mesh
Custom Cell Configuration
Cell Configuration Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
cellSize | 'auto' | number | 'auto' | Number of peers per cell. 'auto' calculates based on network size |
bridgesPerEdge | number | 2 | Redundant connections between adjacent cells |
maxCellSize | number | 50 | Upper limit for cell size |
targetCells | number | 100 | Desired number of cells in large networks |
debug | boolean | false | Log mesh topology and routing |
How Cellular Mesh Works
Inter-Cell Bridges
Designated “bridge” peers connect adjacent cells, enabling cross-cell communication.
When to Use Cellular Mesh
| Scenario | Recommendation |
|---|---|
| Small team (< 50 peers) | Standard mesh (rtc: true) |
| Medium rooms (50-100) | Either works |
| Large events (100-500) | Cellular mesh (cells: true) |
| Massive scale (1000+) | Cellular mesh with custom config |
Connection Lifecycle
Peer Events
Listen for peer connections and disconnections:Monitoring Connection Status
Data Channels
In addition to database sync, you can create custom data channels for ephemeral messaging:Creating Channels
Use Cases for Data Channels
Cursor Positions
Live cursor tracking in collaborative tools
Typing Indicators
“User is typing…” notifications in chat apps
Game State
Real-time player positions and actions
Presence
Online/offline/away status updates
Media Streaming
Audio Streaming
Video Streaming
Production Best Practices
1. Use Multiple Relays
2. Deploy TURN Servers Regionally
3. Monitor Connection Quality
4. Handle Reconnections
5. Test with WebRTC Checker
Before deploying, test WebRTC support:- Visit browserleaks.com/webrtc
- Verify STUN/TURN connectivity
- Check for IP leaks if privacy is important
Debugging P2P Issues
Enable Debug Logging
Common Issues and Solutions
Peers Not Connecting
Causes:
- Relay server down
- Firewall blocking WebRTC
- No TURN server configured
- Check relay status
- Add TURN servers
- Test on different network
Slow Sync
Causes:
- High latency to relays
- Large oplog backlog
- Bandwidth constraints
- Use geographically closer relays
- Reduce
oplogSize - Enable cellular mesh for large networks
Frequent Disconnects
Causes:
- Unreliable network
- Mobile browser in background
- NAT timeout
- Implement keepalive
- Use multiple TURN servers
- Handle reconnection gracefully
Data Not Syncing
Causes:
- Security Manager blocking operations
- Conflict resolution discarding updates
- Middleware filtering data
- Check user permissions
- Verify HLC timestamps
- Review middleware logic
Related Resources
P2P Sync Concepts
Understand how delta sync and full-state fallback work
Security Model
Configure RBAC and cryptographic verification
Chat Example
See P2P networking in a real-time chat app
Video Streaming Example
Build a video conferencing app with GenosRTC