WebRTC: Scaling P2P Mesh Networking for Real-time Video
In 2019, video chat is no longer a luxury feature; it's a standard requirement for many apps. But building a video infrastructure is expensive. The common approach is to use an SFU (Selective Forwarding Unit) or an MCU (Multipoint Control Unit)-dedicated servers that handle all the media streams.
But what if you don't have the budget for a server farm? In 2019, we're looking at WebRTC Mesh Networking.
What is a Mesh Network?
In a WebRTC mesh network, every participant connects directly to every other participant. There is no central media server. Each participant sends their stream to $N-1$ other participants and receives $N-1$ streams.
The Problem: $N^2$ Complexity
The bandwidth and CPU requirements of a mesh network grow quadratically ($O(N^2)$). For a 3-person call, it's fine. For 4 people, it starts to lag. By 6-8 people, it becomes unusable for most users.
Practical Example: Establishing a Connection
The most difficult part of WebRTC in 2019 is the signaling process-how two peers find each other. We use a "Signaling Server" (usually over WebSockets) to exchange SDP (Session Description Protocol) and ICE Candidates.
// A simple peer-to-peer setup with an RTCPeerConnection
const configuration = { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] };
const pc = new RTCPeerConnection(configuration);
// Add local stream
const localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
localStream.getTracks().forEach(track => pc.addTrack(track, localStream));
// ICE Candidate exchange
pc.onicecandidate = ({ candidate }) => {
if (candidate) {
sendToSignalingServer({ type: 'candidate', candidate });
}
};
// Handle remote stream
pc.ontrack = (event) => {
const [remoteStream] = event.streams;
remoteVideo.srcObject = remoteStream;
};
// Create an offer
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
sendToSignalingServer({ type: 'offer', offer });
Optimizing Mesh Performance
To make mesh networks work in 2019, we use several tricks:
- Bitrate Scaling: Lowering the video resolution and bitrate as the number of participants increases.
- Audio Dominance: Only subscribing to the video of the active speaker.
- VP9 and AV1: Exploring more efficient codecs to reduce bandwidth usage.
WebRTC mesh is the "democratic" way to build real-time communication. It's decentralized and cost-effective. While it won't replace massive SFUs for large webinars, for small teams and direct 1-on-1 calls, it is a powerful and viable solution in 2019.
Aunimeda builds production-grade backend systems - APIs, microservices, real-time applications, and system integrations.
Contact us for backend engineering services. See also: Custom Software Development, Web Development