Overview
Ant Media Server supports peer-to-peer (P2P) WebRTC communication, enabling direct connections between clients with the server acting as a signaling coordinator. This mode is ideal for video conferencing, multi-party calls, and interactive applications.P2P Modes
Ant Media Server offers two primary P2P modes:1. Mesh P2P (Peer-to-Peer)
Direct connections between all participants. Each peer sends and receives streams from every other peer. Pros:- Ultra-low latency
- No server bandwidth consumption for media
- Direct peer communication
- Limited scalability (recommended max 4-6 participants)
- High client bandwidth requirement (N-1 uploads per peer)
2. Conference Mode (MCU/SFU)
Server-side mixing or forwarding of streams. The server handles stream distribution. Pros:- Scalable to many participants (100+ participants)
- Lower client bandwidth (single upload)
- Consistent quality for all participants
- Slightly higher latency than mesh
- Server bandwidth consumption
Quick Start: P2P Room
Basic P2P Example
HTML Setup for P2P
Conference Mode
Joining a Conference Room
Conference Room Events
Advanced P2P Features
Multi-Track Conference
Publish and receive multiple tracks (e.g., camera + screen share):Data Channel in P2P
Send text messages or data between peers:Audio/Video Control
Mute/unmute audio and video during conference:Room Management
Get room information and participant list:Signaling Protocol
P2P Signaling Flow
The P2P signaling involves WebSocket messages between peers and server:Conference Signaling Flow
Best Practices
1. Limit Mesh P2P Participants
2. Handle Network Quality
3. Graceful Disconnection
4. Audio Echo Cancellation
Complete Conference Example
Troubleshooting
Common Issues
Issue: Peers cannot connect- Verify both peers have proper ICE configuration
- Check if TURN server is needed for restrictive networks
- Ensure WebSocket connection is stable
- Enable echo cancellation in media constraints
- Mute local audio element (use
mutedattribute) - Use headphones to prevent feedback
- Use conference mode instead of mesh for >4 participants
- Reduce video resolution
- Limit frame rate in constraints
Next Steps
WebRTC Overview
Learn about WebRTC architecture and signaling
Publishing Streams
Learn how to publish WebRTC streams
