---
title: "Streaming a Device Screen to the Browser with WebRTC: Concepts and Architecture"
description: "A WebRTC architecture for streaming a phone screen to the browser with low latency: signaling, STUN/TURN, SFUs, codec choices and a data channel for touch input."
url: https://sanderyaz.com/en/blog/webrtc-device-screen-streaming/
author: Sander Yazıcıoğlu
date: 2026-12-02
updated: 2026-12-02
lang: en
tags: ["WebRTC","Realtime","WebSocket","Mobile"]
---

# Streaming a Device Screen to the Browser with WebRTC: Concepts and Architecture

The best-loved feature of a real-device testing platform is seeing a remote phone live in the browser and tapping on it. Sending screenshots a few times a second isn't enough; users notice even a one-second delay. This article explains, at the concept level, how such a live stream is built with **WebRTC**.

## Why WebRTC?

| Method | Typical latency | Notes |
|---|---|---|
| Periodic screenshots (HTTP) | 1 s and up | Simple but choppy, heavy on bandwidth |
| JPEG/MJPEG over WebSocket | A few hundred ms | Easy to build, inefficient compression |
| HLS / DASH | Several seconds | Great for broadcast, too slow for interaction |
| WebRTC | Usually 100–300 ms | Best fit for interactive use |

WebRTC is the realtime media stack built into the browser: video codecs, bitrate adaptation to network conditions, packet-loss recovery and NAT traversal come out of the box.

## The building blocks

### 1. Capturing and encoding the screen

On the device side, the screen has to become a video stream. On Android this is usually done with tools that produce an H.264 stream from the device's hardware encoder (the approach scrcpy uses); on iOS through the screen-recording stack. Using the device's hardware encoder gives a high frame rate without burning server CPU.

### 2. Signaling

WebRTC doesn't define how two peers find each other; that's your job. The peers exchange an **SDP offer/answer** and **ICE candidates** over a signaling channel. WebSocket is ideal for that channel:

```js
const pc = new RTCPeerConnection({ iceServers: [{ urls: 'stun:stun.example.com:3478' }] });

pc.ontrack = (event) => { video.srcObject = event.streams[0]; };
pc.onicecandidate = ({ candidate }) => candidate && signal.send(JSON.stringify({ type: 'ice', candidate }));

signal.onmessage = async ({ data }) => {
  const message = JSON.parse(data);
  if (message.type === 'offer') {
    await pc.setRemoteDescription(message.sdp);
    const answer = await pc.createAnswer();
    await pc.setLocalDescription(answer);
    signal.send(JSON.stringify({ type: 'answer', sdp: pc.localDescription }));
  }
  if (message.type === 'ice') await pc.addIceCandidate(message.candidate);
};
```

The patterns in [realtime systems with Node.js and WebSocket](/en/blog/websocket-realtime-nodejs/) apply here too, for a signaling connection that holds up.

### 3. NAT traversal: STUN and TURN

Users sit on corporate networks, mobile carriers and behind firewalls. A **STUN** server lets a client learn its public address, and most connections succeed with that. When a direct connection fails, a **TURN** server relays the traffic. If you have enterprise customers, offering TURN over TCP/TLS on port 443 is the most reliable way through strict firewalls.

### 4. Many viewers: an SFU

If several people watch the same device at once (say, in a team meeting), sending a separate stream from the device to each viewer doesn't scale. An **SFU** (Selective Forwarding Unit) receives the stream once and forwards it to every viewer. mediasoup, Janus, LiveKit and Pion-based servers are common choices.

### 5. A data channel for input

Taps, swipes and keyboard input can travel over WebRTC's **RTCDataChannel**, on the same connection as the video, with low latency. Order matters for commands, but re-sending a stale swipe is pointless, so a channel with limited retransmits is a good balance:

```js
const control = pc.createDataChannel('control', { ordered: true, maxRetransmits: 2 });
control.send(JSON.stringify({ type: 'tap', x: 0.42, y: 0.77 }));
```

Sending coordinates as **ratios between 0 and 1** rather than pixels makes the same command land in the right place on any screen resolution.

## Tuning quality

- **Codec:** H.264 is the most common choice thanks to hardware support. VP8/VP9 and AV1 are alternatives depending on browser support.
- **Resolution and frame rate:** 720p at 30 fps is usually enough for testing. Dropping the frame rate on static screens saves a lot of bandwidth.
- **Adaptive bitrate:** WebRTC adapts bitrate to the network automatically, but giving the encoder a ceiling keeps playback smooth on weak connections.
- **Latency monitoring:** collect RTT, frame loss and jitter regularly with `pc.getStats()` and show them, so when a user says "it's slow" you know what is slow.

## Security

- Authenticate the signaling channel, so only the user who reserved a device can join its session.
- Issue short-lived TURN credentials; a fixed username and password lets others use your relay.
- WebRTC media is encrypted with DTLS-SRTP by default; if you use an SFU, make sure it runs on trusted infrastructure too.

## Conclusion

WebRTC is the strongest way to bring a low-latency, interactive screen stream to the browser. With solid signaling, STUN/TURN, an SFU where needed and a data channel for input, a remote phone feels like it is on your desk. To build automated tests on top of that stream, see the [Appium real-device testing guide](/en/blog/appium-real-device-testing/).

---

About the author: [Sander Yazıcıoğlu](https://sanderyaz.com/en/about/). Sander Yazıcıoğlu is a Senior Full-Stack Developer in Istanbul. At LiveTesting he builds the backend, frontend, realtime systems and Appium automation of a real-device testing platform; before that he built enterprise Angular apps at Testinium and Loadium.
