Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 36 ยท Security and Applications

WebRTC in Real Apps: Reconnection, Scaling and E2EE

Move WebRTC from demo to product: recover from network changes with ICE restarts and backoff, scale signalling, add end-to-end encryption with encoded transforms, and plan monitoring.

A demo works on one laptop with a good connection. A product has to survive Wi-Fi to 4G hand-offs, sleeping laptops, flaky hotel networks, thousands of simultaneous rooms and privacy requirements. The WebRTC API gives you the tools; the architecture around them is yours to build.

This lesson collects the practices that separate a demo from a dependable service: reconnecting after a network change, designing signalling that scales, sending end-to-end encrypted media through an SFU, and the supporting systems you will want in place before launch.

The shape of a production system

flowchart TD
  U["Browser or mobile app"] -- "wss: signalling, auth" --> SIG["Signalling service (stateless, behind a load balancer)"]
  SIG --> PUB[("Pub/sub or Redis: room state")]
  U -- "UDP/TCP/TLS media and data" --> SFU["SFU cluster"]
  U -. "relay when needed" .-> TURN["TURN servers"]
  SIG --> AUTH["Auth service: tokens, TURN credentials"]
  SFU --> MET["Metrics and logs: stats, events"]
  U -- "getStats summaries" --> MET

Signalling, media and monitoring are separate systems that scale independently.

Reconnecting: ICE restarts

Networks change during a call: the laptop moves from Wi-Fi to Ethernet, a phone switches towers, the user's public IP changes. The existing candidate pair stops working. connectionState goes disconnected, and if nothing recovers it ends in failed.

An ICE restart gathers fresh candidates and re-runs connectivity checks without tearing down the call. Steps:

  1. Detect trouble: connectionState === 'disconnected' for a few seconds, or 'failed'.
  2. Call pc.restartIce(). This triggers negotiationneeded.
  3. Create a new offer (the offer now has fresh ICE credentials), set it, send it through signalling, and apply the answer.
  4. Media and data channels continue on the new path.

You can also request it explicitly with pc.createOffer({ iceRestart: true }). Either side may start the restart; use a "perfect negotiation" style design so both sides cannot collide on offers.

JS Restart ICE on a live connection

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The data channel kept working across the restart: only the network path was re-selected.

When to give up and rebuild

An ICE restart fixes network path problems. If the peer vanished completely (tab closed, process killed), no restart will help. Use a retry policy:

  • Wait 3 to 5 seconds in disconnected before restarting; many short drops recover alone.
  • Retry restarts with exponential backoff and jitter (1 s, 2 s, 4 s, ... with a random offset) so a server outage does not trigger a thundering herd of reconnections.
  • After a maximum time (for example 30 s) or number of attempts, create a brand-new RTCPeerConnection and rejoin through signalling.
  • Show the user what is happening ("Reconnecting...") and keep local media running so they stay visible to themselves.
JS Stand-in: backoff schedule with jitter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Scaling signalling

Signalling traffic is small, but there are many connections. Principles:

  • Keep servers stateless. Store room membership in Redis or a similar store, and fan messages out through pub/sub so any server can deliver to any client.
  • Use sticky routing only if you must. WebSocket connections are long-lived; a load balancer must support them and drain gracefully on deploys.
  • Authenticate the socket with a short-lived token from your auth service, and check authorisation per room.
  • Rate-limit and size-limit messages. SDP is a few KB; reject anything larger.
  • Ordering matters: process offers, answers and candidates for one peer in sequence.
  • Fallback transport. Some networks block WebSockets; long-polling or WebTransport can be fallbacks.

A signalling service is a small, boring system, which is a good thing. The signalling server project builds one.

End-to-end encryption with an SFU

An SFU decrypts SRTP to forward it (Security: DTLS and SRTP). To keep the SFU blind, the sender encrypts each encoded frame before it enters RTP, and receivers decrypt after. Browsers expose this as encoded transforms (RTCRtpScriptTransform, with older createEncodedStreams in Chromium). The transform runs in a Worker and sees each encoded frame as an ArrayBuffer.

Read-only sketch (it needs a Worker file, a key-management scheme and a browser that supports it, so it cannot run in this preview):

JavaScript
// main thread (read-only sketch)
const worker = new Worker('e2ee-worker.js');
const sender = pc.addTrack(videoTrack, stream);
sender.transform = new RTCRtpScriptTransform(worker, { operation: 'encrypt' });

// e2ee-worker.js
onrtctransform = (event) => {
  const { readable, writable } = event.transformer;
  const op = event.transformer.options.operation;
  readable
    .pipeThrough(new TransformStream({
      async transform(frame, controller) {
        frame.data = await (op === 'encrypt' ? encrypt(frame.data) : decrypt(frame.data));
        controller.enqueue(frame);
      },
    }))
    .pipeTo(writable);
};

The hard part is not the transform, it is key distribution: how members agree on keys, rotate them when someone leaves, and verify identities. Standards such as SFrame and MLS address this. Treat E2EE as a project of its own.

JS Feature detection for newer APIs

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Mobile and cross-platform notes

  • Native apps use the same protocol through libwebrtc wrappers (Android, iOS) or SDKs from your SFU vendor. Signalling stays your own.
  • Backgrounding suspends cameras and sockets on phones. Handle visibilitychange, stop video, and reconnect on resume.
  • Safari is strict: it requires user gestures for playback and can differ in codec support. Test on real devices.
  • Battery and heat: lower resolution and frame rate on small tiles, and avoid simulcast encodes you do not need.

Operations: before launch

  1. Metrics: call setup success rate, time to connected, relay usage percentage, loss/jitter/RTT percentiles, reconnections per call.
  2. Logs: state transitions and error names per session id, never raw credentials.
  3. Load tests with synthetic clients against your SFU and signalling.
  4. Regional TURN/SFU deployments close to users; latency matters.
  5. Failure drills: kill an SFU node, block UDP, expire credentials, change networks mid-call.

Common mistakes

  • No reconnect logic. The first Wi-Fi hiccup ends the call.
  • Restarting immediately on disconnected. Wait a few seconds.
  • Stateful signalling servers that lose rooms on deploy.
  • Treating E2EE as a toggle. Key management is the real work.
  • Only testing on desktop Chrome.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does pc.restartIce() accomplish?

    An ICE restart gathers new candidates and negotiates fresh ICE credentials while keeping media and channels.
  2. 2. Why add jitter to a reconnect backoff?

    Random variation spreads retries over time and prevents a thundering herd.
  3. 3. How does end-to-end encryption through an SFU work?

    Encoded transforms let the sender encrypt frames; receivers decrypt them, leaving the SFU unable to read them.
  4. 4. What is the best way to keep signalling servers scalable?

    Stateless servers behind a load balancer can be added or replaced freely when room state lives elsewhere.

Summary

  • Recover from network changes with a short wait, restartIce(), backoff with jitter, and finally a fresh connection.
  • Keep signalling stateless, authenticated and size-limited; separate it from media and TURN.
  • E2EE with an SFU uses encoded transforms, but key management is the hard part.
  • Detect features instead of browsers, test on mobile, and monitor setup success, relay share and quality percentiles.

Next: Project: Peer-to-Peer Chat