How two browsers talk to each other without a server in between
WebRTC lets one browser send video, sound, messages and files straight to another. This page shows how the connection is made, step by step with a real one, and then lets you connect two browsers by hand and use it.
1Why it is hard
A web page can call out to a server, but nothing can call a web page. Your computer usually sits behind a router that shares one public address among every device in the house, and the router only lets in traffic that answers something sent out first. Two browsers that both sit behind such routers cannot simply dial each other: neither knows the other's address, and neither router would let a stranger in.
2The three things a connection needs
A description of each side
What each browser wants to send (data, sound, video, in which formats) and the fingerprint of the encryption key it will use. This is a block of text called SDP. The first one is the offer, the reply to it is the answer.
Addresses to try
Every way the browser might be reached: its address on the local network, and its public address as seen from outside. Each is an ICE candidate. The two browsers then try every pair until one gets through.
A way to pass those across
The offer and answer have to reach the other side before any connection exists. This is signalling, and WebRTC leaves it entirely to you. Most sites use a server for it. On this page it is you, with copy and paste.
The trick that gets through the routers: once both sides know each other's candidates, both start sending small test packets to each other at the same moment. Each router sees its own computer sending out first, so it lets the replies in. This is often called hole punching.
3Watch a real connection being made
Below, Alice and Bob are two real WebRTC connections inside this one page. Nothing is faked: the text shown is what your browser actually produced. Here the offer and answer are carried across by the page itself; in section 5 you carry them.
Aliceidle
Bobidle
4So is there really no server?
Honestly: it depends on which part you mean.
| Part | Usual way | On this page |
|---|---|---|
| Signalling passing offer and answer | A web server both browsers are connected to | No server. You copy two codes across by chat or mail. |
| STUN finding your public address | A small public server answers one question: “what address do you see me coming from?” | Optional, and only needed across the internet. It never sees your data and is not involved after the answer. On one network you can switch it off. |
| TURN relay of last resort | When hole punching fails (some company and mobile networks), a relay server forwards everything | None. If both networks are of the strict kind, the two browsers here cannot connect. |
| The data itself | Goes directly from one browser to the other, encrypted. Each side checks the other's key against the fingerprint that came with the offer or answer, so nobody in the middle can listen in unless they also changed the codes you passed across. | |
5Connect two browsers yourself
Open this page in a second browser: another computer, a phone, or just another window. One of you hosts, the other joins. The codes are the offer and the answer from section 3, compressed.
1. Send this invitation to the other person
2. Paste the reply they send back
1. Paste the invitation you were sent
Chat
Text goes over a data channel: a two-way pipe for whatever bytes you like. This one is set to deliver every message, in order.
File sharing
A second data channel carries files. A file is cut into 16 KB pieces, sent in order and put back together on the other side. The sender waits whenever the channel's queue fills up, so a large file does not swamp the connection.
Camera and sound
Video and sound do not use data channels. They travel as media tracks, which may drop a late frame instead of waiting for it, and adjust their quality to the connection. Room for one sound track and one video track each way was agreed in the offer and answer, so switching the camera on later needs no new exchange of codes.
With both browsers on one computer, a microphone will howl: use the test tone, or headphones.
What the connection looks like
6What is inside a code
A code from section 5 is nothing secret or magic. It is the offer or answer text, squeezed with ordinary compression and written in letters that survive being pasted into a chat. Paste one here to read it.
7Things worth knowing
A code is for one connection
The offer and answer belong to one attempt. If it fails or you close the page, start again with fresh codes.
Why making a code takes a moment
The browser waits until it has found all its addresses, so that they fit in one code. Sites with a signalling server send each address as it is found instead, which is faster.
Two is easy, ten is not
Every pair needs its own connection. Calls with many people usually send everything through a server in the middle, which is no longer peer to peer.
Your address is visible
The other browser learns an address it can reach you at. That is how a direct connection works. Browsers hide local addresses behind random .local names where they can.