📚 Encyclopedia

Web Development · updated 2026-11-16

1:1 WebRTC voice calls over polled signaling and TURN

webrtcvoipturncoturnsignalingflutter-webrtcpush

A WebRTC call needs three things besides the media itself: a signaling channel to swap session descriptions (SDP) between the two peers, ICE servers (STUN to learn your public address, TURN to relay when no direct path exists), and a way to ring a device that is not looking. None of them needs a WebSocket server. TLDR's v1.63.0 voice calls (1:1, audio only, Flutter web + Android via flutter_webrtc) use a plain REST table for signaling, coturn on the same 1 GB VPS, and an FCM push to ring.

Signaling through a polled table

Skip trickle ICE when signaling is slow

Trickle ICE sends each candidate as its own signal as it is discovered. Over a polled mailbox that is many round trips. Instead, wait for iceGatheringState == complete (capped at ~4 s so a slow interface cannot stall the call), then send getLocalDescription(), which by then contains every candidate. One offer and one answer per call.

The signal vocabulary

signal direction meaning
offer caller → callee SDP + the caller's device id; rings the callee
answer callee → caller SDP + which device picked up
taken caller → callee another of your devices answered; stop ringing
decline, busy callee → caller busy = already on a call (sent automatically)
cancel caller → callee caller hung up before an answer
hangup either call over

Multi-device: a user may be signed in on a phone and a browser. Each app run gets a random device id, every device rings, and the caller sends taken once one device answers. Ring timeout 45 s.

TURN credentials without user accounts

coturn's use-auth-secret mode (the "TURN REST API" scheme) needs no per-user configuration: - The API mints a short-lived credential: username = "<expiry-unix>:<random>", credential = base64(HMAC-SHA1(secret, username)), TTL about 10 minutes. - coturn holds the same secret as static-auth-secret and rejects expired usernames. Keep the secret in a server-only config file outside git. - Return both turn:host:3478?transport=udp and ?transport=tcp; fall back to a public STUN server if the TURN endpoint fails so a direct path still works. - The relay only ever sees DTLS-SRTP packets, so it cannot listen in.

Connection state handling

Ringing a device that is not open

End-to-end encryption of signaling

If the signaling blobs are encrypted end to end, the server sees neither the SDP nor the DTLS fingerprint inside it, so it cannot substitute its own fingerprint and man-in-the-middle the media. Details of the key derivation are in Simple E2E messaging with X25519 + HKDF + AES-GCM (and why not Signal).

Testing

Two headless Chrome instances with --use-fake-device-for-media-stream and --use-fake-ui-for-media-stream can run a full ring, answer, connect and hangup cycle. Log in by injecting the stored auth token into localStorage rather than driving a login form, which was flaky. This does not test a real NAT path through TURN or a killed app being woken by the push; those need real phones on different networks.

When calls ring but never connect

The first real calls between two homes failed six times in a row, two weeks after release. Signaling was perfect every time (offer, answer, taken), then the callee hung up 10 to 11 seconds after answering: that is ICE timing out, not a signaling bug. The lessons:

coturn settings that bite later: - denied-peer-ip covering the server's own public address blocks relay-to-relay pairs, which is the path two clients take when both sit behind symmetric NAT. - user-quota and total-quota count allocations, not calls. A phone with several network interfaces, each trying two TURN URLs, uses several allocations per call; quotas of 4 and 12 are tight.

A VoIP call inside an ordinary app also knows nothing about the phone's own calls. Without the Android telecom framework (a ConnectionService), audio-focus handling or phone-state listening, a cellular call that arrives mid-call is not coordinated with it: neither call is held or ended, and Android hands the microphone to the cellular call. See Writing a replacement phone (dialer) app for Android for the telecom side.

Gotchas

See also

Linked from

Sources: tldr memory (feature-voice-calls), tldr post #1861, tldr repo (call_service.dart, call_manager.php), tldr post #1897