Skip to content

feat: sync over WebRTC with Firebase signaling and no Google login (WIP) - #9654

Draft
pjwerneck wants to merge 16 commits into
devfrom
pjwerneck/webrtc-signaling-server
Draft

pjwerneck wants to merge 16 commits into
devfrom
pjwerneck/webrtc-signaling-server

Conversation

@pjwerneck

@pjwerneck pjwerneck commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • When a DO or DS logs in with login, login_do or login_ds, it previously synced only over Google Drive, after a Google login. These functions are in syft and in syft-rds. Now webrtc=True syncs over WebRTC, with no Google login. Two settings name the Firebase project: SYFT_FIREBASE_PROJECT_ID and SYFT_FIREBASE_API_KEY. The owner of the project gives both values to its users. The owner is OpenMined, or an org. Neither value is in the source.
  • When a user logs in with webrtc=True for the first time on a machine, Firebase sends a sign-in link to the email. The user pastes the link into the notebook once. If Firebase later refuses the saved session, the next login sends a new link. webrtc=WebRTCConfig(...) selects a project, or the Firebase emulators.
  • User-facing API change: login, login_do, login_ds, SyftboxManagerConfig.for_colab and SyftboxManagerConfig.for_jupyter get a new webrtc parameter.
  • When a DS asks a DO to peer in WebRTC mode, the request now goes through the Firebase Realtime Database. The database also holds acl/, the key bundles and the peer info. acl/ lists the peers that each user accepts. The rules in infra/firebase/database.rules.json control who reads and writes each path. Before, WebRTC worked only in test pairs, with peering and the state of the DO on a mock Drive. The peer list stays on local disk, with the version entry of SYFT_peers.json.
  • When two peers that accept each other are online, they now find each other through the database. Each client reads its own signal/ node as one stream of server-sent events, so nothing polls. A client says hello to each peer that has no open channel to it. Then the peer with the lower email sends the WebRTC offer. Only an accepted peer can send signals, and each sender has one hello slot. Before, WebRTC peers found each other only through LocalSignaling, in one process.
  • When a client opens a peer connection, it now gets new Cloudflare TURN credentials from the Cloud Function of the project. The credentials work for 4 hours. Before they expire, the side that sent the offer opens a new connection.
  • When add_peer, approve_peer_request or load_peers changes the peers, the WebRTC connection now gets the new list of peers. It connects to new peers, and it closes the channels to removed peers.
  • When the signaling session ends, the next sync() now raises the error at its end. The same happens when a connect, a hello or an answer fails in the background, or when a peer refuses an offer.
  • When a DO syncs in WebRTC mode, its own state stays on local disk: the events, the checkpoints, the incrementals and the rolling state. Drive mode keeps this state on Drive, as before.
  • When a WebRTC client makes a dataset call, it gets NotImplementedError, because datasets do not go over WebRTC yet. Therefore syft-rds registers no dataset collection specs for a WebRTC client.
  • When AiortcPeerConnections sent a frame, it previously sent the whole frame as one data channel message. That message was larger than the 64 KiB limit of aiortc. The aiortc receiver needs quadratic time for such a message, so a message of 100 MB never arrived. Now AiortcPeerConnections splits each frame into chunks of 64 KiB or less, and the peer joins the chunks again.
  • New: the infra/firebase/ folder, which the owner of a project deploys. It holds the database rules, the turn_credentials Cloud Function, and the setup steps in infra/firebase/README.md. We run no server.

Guidelines

High level

  • 🟢 Split: the chunk fix, the Firebase signaling and peering, and the login serve one flow. In that flow, a DO and a DS on different networks sync over WebRTC.
  • 🟢 Size: this PR lets login_do and login_ds sync over WebRTC through a Firebase project, with no Google login.

Mid level

  • 🟢 User-facing code
  • 🟢 Error handling
  • 🟢 State versioning
  • 🟢 Function structure
  • 🟢 Tests

Low level

  • 🟢 Naming
  • 🟠 Code style: SyftboxManager._webrtc_connections imports aiortc inside the function. The reason is the optional webrtc extra, not a circular import: a top-level import would break import syft without that extra.

Security

  • Firebase Auth proves the email of each user with an emailed sign-in link. The database rules let each user write only under its own email.
  • Only a peer that the receiver accepts in acl/ can send signals to the receiver. Each type of signal has exactly its own fields. A hello can only replace the hello slot of its sender.
  • Any user who controls an email can sign in to a project. Then that user can send peer requests, read key bundles and get TURN credentials. The key directory gives no list of users, and the TURN function has no rate limit per user.
  • The admins of the project can read and change all data, so they can swap a key bundle. Users who do not trust the project use Drive.
  • Messages go from peer to peer over DTLS. A TURN relay forwards only encrypted packets.
  • The Cloudflare TURN key stays in the Secret Manager of the project. Clients get credentials that expire after 4 hours.
  • The project ID and the web API key come only from the environment. No credential is in versioned source.
  • Drive mode does not change.

Testing

  • The unit tests are in tests/unit/webrtc/, tests/unit/test_login_webrtc.py and packages/syft-rds/tests/test_webrtc_config.py. They cover the signals in stream events, and how the stream handles an unreachable token refresh and auth_revoked. They also cover the ID token after the machine sleeps, the sign-in link and the session file. Other unit tests cover a failed sign-in, the ICE servers that the TURN function picks, and the login settings.
  • The integration tests in tests/integration/webrtc/ run on the Firebase emulators. They cover the database rules, the stream, DatabaseSignaling with aiortc clients on localhost, and DatabasePeeringConnection. They also cover the login with the emailed link, and the job flow of a DO and a DS from end to end. Other tests cover a DO with two DSs at once, and an offer that waits in the inbox from before the sign-in. The client deletes that offer, and does not answer it. The tests passed three times in a row. They need npx and Java 21. The CI job webrtc-tests runs them.
  • A test drops all packets between two aiortc clients on localhost, until both channels close. At the next set_peers, the two clients connect again.
  • On the real project, the sign-in with a real emailed link worked, and so did the token refresh and the refusals of the rules. A hello arrived over the stream in about 0.4 s. The deployed function gave TURN credentials for 4 hours.
  • Two clients on one machine used the deployed TURN function, with only relay candidates in their offer and answer. Both sides connected relay to relay, and 10 MB went through Cloudflare at 4.5 MB/s.
  • In a demo across two networks, the DO ran on a home network, and the DS ran on a laptop on a phone hotspot. Peering, a bash job and its result went through with no Drive, over a direct WebRTC channel. Nobody has run a client on Colab yet.

Asana task

aiortc advertises a max-message-size of 64 KiB. RFC 8841 forbids a larger
message, but AiortcPeerConnections sent each frame as one message. aiortc
does not refuse such a message, and its receiver needs quadratic time for
it. Therefore a 100 MB message never arrived.

AiortcPeerConnections now splits each frame into chunks of at most 64 KiB,
and the peer joins the chunks again. A new test checks the size of each
message that aiortc sends.
Each client keeps one WebSocket open to the server. The server relays
offers and answers between two online clients by email. It tells a client
when a watched peer comes online or goes offline. It gives each client
short-lived TURN credentials from Cloudflare. Clients sign in with one
shared access key, and the server keeps all state in memory.

Run it with python -m syft.sync.connections.webrtc.signaling.server. The
webrtc extra now includes websockets.
…ing code

The signaling server reads and checks a client's hello in its own helper.
The checks of the watch list and the offline error moved into helpers
too. Each function name now says what the function does. The server uses
the CloseCode of websockets in place of its own copy of 1008, and the
docstrings follow the Google style.
…e online

ServerSignaling signs in to the signaling server over one WebSocket and
carries offers and answers through it. AiortcPeerConnections gets its ICE
servers from the signaling. Its new watch() connects to a watched peer each
time the peer comes online. Of two peers, the one whose email sorts first
sends the offer.
A connection to the signaling server carries one signaling session or one
peering request. Peering requests store and read peer requests, and the key
bundles and peer info that a client shares with a peer.
ServerPeeringConnection uses them and keeps the peer list on local disk. The
server matches emails in lower case and reports each email as its owner
spells it.
LocalOwnerStoreConnection keeps the event log, the checkpoints and the
rolling state in a folder on local disk. It keeps the order and the
replacement rules of the Drive store. A WebRTC user has no Drive, so it is
the owner store for WebRTC mode.
login_do and login_ds, in syft and syft-rds, take webrtc=True or a
WebRTCConfig. The client then logs in to no Google account. It sends
messages over WebRTC, peers through the signaling server, and keeps the
owner state on local disk. Datasets do not go over WebRTC yet.
webrtc=True reads the server from SYFT_SIGNALING_URL and
SYFT_SIGNALING_ACCESS_KEY.

The DS sends the offers, and the DO answers only the peers it approved.
A client signs in to the server only when it knows its peers.
A signaling session that ends makes the next set_peers raise, and the call
after it signs in again. A cancelled caller no longer cancels the shared
sign-in. Each new peer connection gets new ICE servers, because TURN
credentials expire. Peer emails keep the spelling of the peer list, and a
DO that accepts with add_peer accepts the DS at once.

A refused offer goes back to the DS as a refuse message. Failed connects,
refused offers and ended sessions raise at the next set_peers. syft-rds
passes no dataset specs in WebRTC mode, and the config raises on any.
set_peers no longer raises old background failures, so an approval always
completes and fires peer_approved. sync() raises them at the end of its
pass, after the client has signed in again. A token_path with webrtc
raises. One helper builds the connection router config for both login
builders, and names, docstrings and a comment follow the guidelines.
WebRTC clients no longer use a signaling server of ours. Each org sets up a
Firebase project from firebase/README.md: Firestore holds presence, an inbox
of offers and answers for each client, peer requests, key bundles and peer
info, under the rules in firebase/firestore.rules. A Cloud Function gives
signed-in users short-lived Cloudflare TURN credentials.

The client polls Firestore over REST with the user's Firebase ID token.
webrtc=True reads SYFT_FIREBASE_PROJECT_ID and SYFT_FIREBASE_API_KEY, and the
first login on a machine signs in with an emailed link. A refused session asks
for a new link. The signaling server, its client and websockets go away.

Tests run on the Firebase emulators, which need npx and Java 21.
Comment thread infra/firebase/functions/main.py Fixed
@pjwerneck pjwerneck changed the title feat: sync over WebRTC with Firebase signaling and no Google login feat: sync over WebRTC with Firebase signaling and no Google login (WIP) Nov 24, 2026
@pjwerneck pjwerneck changed the title feat: sync over WebRTC with Firebase signaling and no Google login (WIP) feat: sync over WebRTC with Firebase signaling (WIP) Nov 24, 2026
…nsplit v3 task says

WebRTC signaling and peering now use the Firebase Realtime Database in place
of Firestore. Each client reads its own signal/ node as one stream of
server-sent events, so nothing polls. A client says hello to each accepted
peer with no open channel, and the lower email sends the offer. acl/ lets
only accepted peers write signals, and each sender has one hello slot.

TURN credentials last 4 hours, and the offering side opens a new peer
connection before they expire. The ID token follows the wall clock, so it
stays valid after the machine sleeps. The project ID and the web API key come
only from SYFT_FIREBASE_PROJECT_ID and SYFT_FIREBASE_API_KEY. The Firestore
code goes away.
…C mode

Two new tests run on the Firebase emulators. In one, a DO exchanges frames
with two DSs at once. In the other, an offer that waits in the inbox from
before the sign-in gets no answer. A third test drops all ICE packets until
both channels close. Then the next set_peers must connect the two peers again.

sign_in_failed returns early instead of combining three checks, and prettier
formats firebase/firebase.json.
…t v3 task says

The rules, firebase.json, the TURN Cloud Function and the setup steps move
from firebase/ to infra/firebase/. The docstrings and the tests use the new
path.
@pjwerneck pjwerneck changed the title feat: sync over WebRTC with Firebase signaling (WIP) feat: sync over WebRTC with Firebase signaling and no Google login (WIP) Nov 25, 2026
… reply

When the TURN Cloud Function refused a Firebase ID token, its 401 reply held
the text of the exception. A ValueError from verify_id_token can describe the
setup of the function, and CodeQL flagged it as py/stack-trace-exposure. Now
the reply gives a fixed reason, and the function logs the exception.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants