SwiftClipSwiftClip
Back to Blog

What Is WebRTC? How Peer-to-Peer File Transfer Works in Your Browser

In 2011, Google open-sourced a collection of browser technologies under the name WebRTC — short for Web Real-Time Communication. The core promise was transformative: enable real-time audio, video, and data transfer directly between web browsers, without plugins, without proprietary software, and without requiring data to pass through a central server.

More than a decade later, WebRTC is embedded in virtually every major web browser and powers technologies that billions of people use daily — from Google Meet and Zoom's browser interface to Discord's voice channels and file-sharing tools like SwiftClip's Live P2P mode.

This is a deep dive into how WebRTC works, why it enables a fundamentally different kind of web application, and how peer-to-peer file transfer in the browser actually functions under the hood.


The Problem WebRTC Solves

Before WebRTC, real-time browser-to-browser communication was technically painful. Browsers were fundamentally designed as document viewers — they could request content from servers and display it, but they could not communicate directly with each other.

To enable real-time communication (like a video call) between two browser users, you had to:

  1. Install a plugin (Flash, Silverlight, or a proprietary codec).
  2. Route all media through a central server that received streams from both parties and forwarded them.
  3. Accept high latency, because every packet traveled: Browser A → Server → Browser B.

The server in the middle was simultaneously a bottleneck and a security risk. Your video call, voice chat, or file transfer passed through a third party's infrastructure before reaching its destination.

WebRTC changed this by enabling a direct connection between browsers — peer-to-peer. Once the initial connection is established, data travels directly from Browser A to Browser B, bypassing any central server entirely.


The Three Core Components of WebRTC

1. MediaStream (getUserMedia)

This API gives web applications access to the user's camera and microphone (with permission). It is the component that powers browser-based video calls. For file transfer purposes, this component is not used.

2. RTCPeerConnection

This is the heart of WebRTC. RTCPeerConnection manages the connection between two peers — handling codec negotiation, network path discovery, encryption, and the actual transmission of data. It is complex internally (it manages ICE, STUN, TURN, SDP, and DTLS — more on these below), but it exposes a relatively simple API to developers.

3. RTCDataChannel

This is the component most relevant to file transfer. RTCDataChannel provides a bidirectional data channel between two WebRTC peers, similar to a WebSocket — but peer-to-peer instead of client-server. Data sent through an RTCDataChannel travels directly from one browser to the other, encrypted with DTLS (Datagram Transport Layer Security).

It is through RTCDataChannel that SwiftClip's Live P2P mode transfers files directly between two browsers.


How Two Browsers Find and Connect to Each Other

The most counterintuitive aspect of WebRTC for developers learning it is the signaling process. If two browsers are going to connect directly to each other, how do they find each other in the first place?

The answer is: they use a central server temporarily, only for the initial handshake, and then communicate directly afterward.

The Signaling Phase

The signaling phase involves each browser describing itself to the other: what codecs it supports, what network addresses it is reachable at, and the cryptographic keys for the connection. This description is encoded in a format called SDP (Session Description Protocol).

Each browser creates an SDP "offer" or "answer" and sends it to the other browser through a signaling server. The signaling server is a simple relay — it does not understand or process the SDP, it just forwards it from one peer to the other. Once both browsers have exchanged SDP, they have enough information to establish a direct connection.

SwiftClip's signaling server handles this initial handshake. When you generate a Live P2P code on one device and enter it on another, the SwiftClip server facilitates the SDP exchange. After that, the file transfer itself does not pass through SwiftClip's servers.

ICE: Finding a Path Through NAT

Here is where it gets technically interesting. In the modern internet, most devices sit behind NAT (Network Address Translation) — a router that maps private IP addresses (like 192.168.1.x) to public IP addresses. From the outside internet, your specific device is not directly addressable.

This means two browsers cannot simply exchange their IP addresses and connect. The router is in the way.

WebRTC solves this with ICE (Interactive Connectivity Establishment), which discovers multiple potential network paths between the two peers and selects the best one:

STUN (Session Traversal Utilities for NAT): A STUN server helps each browser discover its public-facing IP address and port. The browser sends a request to the STUN server, which replies with the public IP:port that the request came from. The browser includes this information in its ICE candidates.

TURN (Traversal Using Relays around NAT): In cases where a direct peer-to-peer connection is impossible (some corporate firewalls, symmetric NAT configurations), ICE falls back to a TURN server — a relay that forwards data between the peers. This is similar to the pre-WebRTC model (data passing through a server), but it is only used as a fallback when a direct connection cannot be established.

In most cases on typical home or office networks, WebRTC successfully establishes a direct peer-to-peer connection without needing a TURN relay.


How File Transfer Works Over RTCDataChannel

Once a RTCDataChannel is established between two browsers, it can be used to send arbitrary binary data — including files.

The process for a large file transfer (simplified) looks like this:

1. File Chunking

A large file cannot be sent as a single blob of data. RTCDataChannel has practical message size limits (typically around 64KB for reliable transfer). So the sending browser reads the file in chunks using the FileReader API:

const CHUNK_SIZE = 65536; // 64KB
const file = userSelectedFile;
let offset = 0;

while (offset < file.size) {
  const chunk = file.slice(offset, offset + CHUNK_SIZE);
  const arrayBuffer = await chunk.arrayBuffer();
  dataChannel.send(arrayBuffer);
  offset += CHUNK_SIZE;
}

2. Chunk Transmission

Each chunk is sent through the RTCDataChannel. Because WebRTC's data channel supports both reliable (TCP-like, guaranteed delivery) and unreliable (UDP-like, no guarantee) modes, file transfer uses the reliable mode to ensure no chunk is dropped.

3. Reassembly

On the receiving end, the browser collects incoming ArrayBuffer chunks and assembles them back into the original file:

const receivedChunks = [];

dataChannel.onmessage = (event) => {
  receivedChunks.push(event.data);
  
  if (receivedChunks length === expectedChunkCount) {
    const blob = new Blob(receivedChunks);
    // Create download link from blob
    const url = URL.createObjectURL(blob);
    triggerDownload(url, fileName);
  }
};

4. Progress Tracking

Because the sender knows the total file size and chunk count, it can calculate and report progress (e.g., "Sent 45 of 200 chunks — 22.5%"), which SwiftClip's UI displays as a progress bar during the transfer.


Security in WebRTC Data Channels

All WebRTC data channels are mandatorily encrypted using DTLS (Datagram Transport Layer Security) — the UDP equivalent of TLS. This is not optional. The WebRTC specification requires it, and browsers enforce it. You cannot establish an unencrypted WebRTC data channel in a standards-compliant browser.

This means that when SwiftClip's Live P2P mode transfers your file directly between two browsers, the data is encrypted in transit even without any additional application-level encryption. An adversary who intercepts the network traffic between the two peers would see only encrypted data.

For the signaling phase (the SDP exchange), SwiftClip's server communicates over HTTPS, which provides its own transport encryption.


How SwiftClip Uses WebRTC for Live P2P

SwiftClip's Live P2P mode integrates WebRTC in a way that abstracts all of the above complexity for the end user:

  1. On the sending device, you open the Live P2P tab and drag in your file. SwiftClip generates a code and begins listening for a peer connection.
  2. SwiftClip's server acts as the signaling server — handling the SDP offer/answer exchange and ICE candidate relay.
  3. On the receiving device, you enter the code. The SDP answer is transmitted through SwiftClip's server, and ICE negotiation begins.
  4. Once a direct peer-to-peer connection is established, the file transfer begins entirely between the two browsers. SwiftClip's servers are no longer in the data path.
  5. The file streams from the sender's browser to the receiver's browser in chunks, with a real-time progress indicator on both ends.
  6. When the transfer is complete, the receiver's browser assembles the chunks into the original file and triggers a download.

At no point during the file transfer does the file content pass through SwiftClip's servers. This is what "peer-to-peer" means in a technical sense — not a marketing term, but a specific architectural property of the WebRTC data channel.


Frequently Asked Questions

Does WebRTC file transfer work on mobile browsers? Yes. WebRTC is supported in Chrome for Android, Safari for iOS (since iOS 14.5), Firefox for Android, and Samsung Internet. The implementation quality varies slightly, but basic data channel functionality works on modern mobile browsers.

What is the maximum file size for WebRTC transfer? There is no inherent protocol limit. The practical limit is determined by available RAM on the receiving device (since the browser assembles the entire file in memory before writing it to disk) and connection stability. SwiftClip's Live P2P mode supports files up to 1GB.

What happens if the connection drops during a WebRTC file transfer? Unlike HTTP downloads, WebRTC transfers do not automatically resume after a dropped connection. If the connection drops, the transfer must be restarted from the beginning. For large files on unreliable connections, HTTP-based file transfer (which supports range requests and resumption) is more robust.

Is a WebRTC connection truly peer-to-peer if a TURN server is used? Not fully — when a TURN relay is used as a fallback, data passes through the TURN server. However, the data is still encrypted end-to-end (DTLS), so the TURN server operator sees encrypted packets, not the plaintext content. True peer-to-peer direct connections are established whenever network conditions allow.

Can WebRTC work through a firewall? WebRTC ICE includes multiple fallback strategies. Most home and office firewalls allow WebRTC connections because they use standard HTTPS ports for signaling and common UDP ports for data. Very restrictive corporate firewalls may block direct connections, in which case WebRTC falls back to TURN relay. If even TURN is blocked, the connection cannot be established.


WebRTC represents one of the most significant architectural shifts in the history of the web — from a document retrieval system to a platform capable of real-time peer-to-peer communication. The technology that powers Google Meet, WhatsApp Web calls, and file transfer tools like SwiftClip's Live P2P mode is running directly in your browser, encrypted, standardized, and available without any plugins.

Understanding how it works gives you a clearer picture of what "peer-to-peer" actually means in practice — and why it represents a fundamentally more private and efficient architecture for real-time data transfer than the traditional client-server model.