AboutBlogContact
BackendMarch 20, 2019 2 min read 24

WebRTC: Scaling P2P Mesh Networking for Real-time Video (2019)

AunimedaAunimeda
📋 Table of Contents

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:

  1. Bitrate Scaling: Lowering the video resolution and bitrate as the number of participants increases.
  2. Audio Dominance: Only subscribing to the video of the active speaker.
  3. 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.

Read Also

Bun: SQLite and the Power of Zero-Overhead FFI (2023)aunimeda
Backend

Bun: SQLite and the Power of Zero-Overhead FFI (2023)

Bun isn't just a fast runtime; its native SQLite implementation and FFI are changing how we think about Node.js performance in 2023.

GraphQL Subscriptions: Real-time Data with Redis PubSub (2018)aunimeda
Backend

GraphQL Subscriptions: Real-time Data with Redis PubSub (2018)

Queries and Mutations aren't enough. In 2018, we're using GraphQL Subscriptions to power real-time dashboards and chat apps.

Phoenix Channels: Real-time Web Without the Scalability Headache (2016)aunimeda
Backend

Phoenix Channels: Real-time Web Without the Scalability Headache (2016)

Is Node.js the king of real-time? Not anymore. In 2016, Elixir and Phoenix are showing us how to handle 2 million connections on a single node.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Get Consultation All articles