Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice: WebRTC Exercises With Solutions

Test your WebRTC skills with four hands-on exercises: measure round-trip time, prove ordered delivery, classify candidates and build a stats summary, each with a runnable solution.

You have now seen data channels, STUN and TURN, topologies, quality, security and a few projects. The fastest way to make that knowledge stick is to write code without a script to follow. This page gives you four exercises. For each one, read the task, try it yourself first in a blank editor, and then open the solution.

All exercises run on a loopback connection inside the page, so you need no server and no camera. A small shared helper is repeated in every solution so each editor is self-contained.

Exercise 1: measure round-trip time

Task. Send five ping messages over a data channel. The other side answers each with a pong carrying the same number. Measure the time between each ping and its pong with performance.now(), then print whether all five were answered and whether the median is a sensible number (greater than 0 and less than 1,000 ms).

Hints: use a Map from sequence number to send time, and a negotiated channel so the code is symmetric.

JS Solution 1: ping and pong

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

No output.

Exercise 2: prove ordered delivery

Task. Create an ordered: true channel, send the numbers 0 to 199 as separate messages, and check on the receiving side that they arrive in exactly that order and none is missing. Then explain in one sentence what would change with ordered: false, maxRetransmits: 0 on a lossy network.

JS Solution 2: order check

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

No output.

Exercise 3: classify ICE candidates

Task. Write summarize(lines) that takes raw candidate lines and returns an object counting each type (host, srflx, prflx, relay) and tells whether the set is good enough for a user behind a strict firewall (it needs at least one relay candidate using TCP or TLS). Test it on the sample lines.

JS Solution 3: candidate summary

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

No output.

Exercise 4: a quality verdict from stats

Task. Write verdict(stats) that turns numbers such as rttMs, jitterMs and lossPercent into 'good', 'fair' or 'poor', using these rules: poor if loss is above 8 percent or RTT above 400 ms; good if loss is below 2 percent, jitter below 30 ms and RTT below 150 ms; fair otherwise. Then compute loss percent from two cumulative snapshots, because stats are counters (Reading getStats).

JS Solution 4: verdict from snapshots

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

No output.

Scenario questions

Think through these on paper; there is no code.

  1. Users in one office never connect, while everyone at home does. STUN works. What do you add and what do you test with?
  2. A 12-person meeting is choppy for everyone on mobile. Which architecture and which feature would you introduce?
  3. A peer sends a data channel message containing <img src=x onerror=...>. What does your UI do with it?
  4. You see connectionState: 'disconnected' for two seconds, then 'connected' again. Should your app restart ICE immediately?

Suggested answers: (1) TURN with TCP and turns: on 443, tested with iceTransportPolicy: 'relay'; (2) an SFU with simulcast and last-N speakers; (3) render with textContent, never innerHTML; (4) no, disconnected can heal itself, wait a few seconds first.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why is stats-based loss computed from two snapshots?

    Counters only grow, so the loss in an interval is the difference between two readings.
  2. 2. Which candidate makes a connection possible behind a strict firewall that only allows port 443 over TCP?

    Only a TURN relay reachable through TCP/TLS on an allowed port passes such firewalls.
  3. 3. What does an ordered, reliable channel guarantee?

    Ordered, reliable delivery retransmits lost packets and holds later ones back, so it can add delay.

Summary

  • You measured round-trip time, verified delivery order, classified candidates and judged quality from counters.
  • The same building blocks (loopback pair, negotiated channel, envelopes) solve many problems.
  • Next steps: build your own signalling server, add TURN, try an SFU, and watch real stats in chrome://webrtc-internals.

Next: back to the WebRTC track page