All posts

How I Designed and Built Watermelon Link

Design NotesPublished Updated @beimenjun

Watermelon Backup 1.6 added Backup to Computer, also known as Watermelon Link. There's nothing to install on the computer. Pick a folder in the browser, scan the QR code with your iPhone, and the backup goes straight into that folder over the local network. That's only a few steps for the user. Getting the browser to work with the existing backup system took quite a few more.

It sounds like a simple request

After I built Watermelon Backup, people kept asking whether they could back up their iPhone photos straight to a hard drive on their computer.

Watermelon Backup already supported SMB, WebDAV, S3, SFTP, and external storage. In theory, you could just set up an SMB share on the computer.

But most people don't know what SMB is, let alone how to create an account, share a folder, and get through the firewall. Each step can go wrong. They just want a place to put their photos, and here I am sending them instructions for setting up a file server.

Then there's macOS File Sharing, an SMB server my app couldn't connect to at all. Troubleshooting that could fill an article of its own. When someone goes to the trouble of setting it up only to have it fail, I'm the one who hears about it.

Plugging in a cable sounds like the obvious answer. I thought about writing Windows and macOS clients and then figuring out how they'd talk to the phone. That meant installers, signing, updates, system permissions, and two codebases. The connection might need private APIs, too. An app using those APIs wouldn't make it into Apple's store. So do I bundle the client and ask people to drag it out? Put it on a website? Open more repositories? And if I'm already writing desktop clients, why not design my own protocol? Did I really need all of that?

Then I remembered something I'd come across when working with video calls: WebRTC can send data directly over the local network without routing it over the public internet.

Browsers have also gained the File System Access API. Once you choose a folder, a page can read and write local files within the access you grant it.

Putting those together made things much simpler. The browser could stand in for a desktop app, and WebRTC could connect the phone to the page. I'd get the file access I needed without making anyone install a client.

How a connection comes up

The path through Watermelon Link is straightforward:

Watermelon Backup on iPhone
        │
        │  WebRTC DataChannel
        ▼
Desktop browser ── File System Access API ── Selected folder

        A signaling service passes setup information between the two ends

Open link.watermelonbackup.com, choose a backup folder, and the page shows a QR code. You can scan it with the iPhone Camera app or the scanner in Watermelon Backup. The two ends use the signaling service to exchange connection information. Once the DataChannel is open, photo data and file operations travel over the local network.

There's no STUN or TURN. The two devices have to be on the same local network, and some more complicated networks still won't work. That was a trade-off I was willing to make.

The signaling service is only there to establish the connection. It doesn't receive photos or store the backup. Pairing uses temporary information, and the signaling messages are encrypted.

The front end uses TypeScript and Vite. It handles folder access, QR codes, connection state, and file operations in the browser. The back end is a small Node.js service that relays signaling over WebSocket. Its session state isn't written to disk.

I like that split. The back end stays small, and the photos only travel from the phone over the local network to the user's drive.

Fitting into the existing backup system

Getting a web page to receive a photo and save it to a folder was a quick demo. Making it work with Watermelon Backup's existing pipeline took much longer.

Moving the photo bytes is only part of a backup. The app also handles resource hashes, monthly manifests, the remote index, deduplication, write locks, and recovery from interruptions. A separate backup pipeline for Watermelon Link would mean writing all of that again and maintaining it in two places.

Instead, I added a storage client:

actor BrowserLinkStorageClient: RemoteStorageClientProtocol

The SMB, WebDAV, S3, and SFTP clients in Watermelon Backup all implement RemoteStorageClientProtocol. By working at that same layer, Watermelon Link looks like another storage Node to the backup pipeline. Directory listings, metadata reads, folder creation, moves, copies, uploads, and downloads go to the browser over the DataChannel. The browser carries them out with the File System Access API. While connected, you can also browse existing backups on the computer and download items from them.

That saved a lot of duplicate code. The existing index, deduplication, and recovery logic kept working. Most of the new code could stay in the connection and transport layers.

The app doesn't save Watermelon Link Nodes. When you close the page or disconnect, the Node disappears from the session, and scheduled background work won't wake it up. If a backup is already running with Picture-in-Picture (PiP) enabled, an active transfer can keep going for a while through the existing PiP mechanism. It feels more like a network drive you've plugged in for a moment.

That was when I knew the idea was worth pursuing. Inside Watermelon Backup, the browser had a full storage interface. This wasn't just a file upload form anymore.

Reworking the transfer protocol

The first protocol was straightforward. I split files into 32 KiB chunks, encoded each as Base64 in JSON, and waited for the browser to write one chunk before the phone sent the next.

It was easy to debug because every request showed up in the console. Large videos made the drawbacks obvious. Base64 added overhead, and waiting for a reply after every chunk kept throughput low.

There's another easy detail to miss: success from DataChannel.send() means the data entered the send queue. It doesn't mean the browser has finished writing it to disk.

I separated control messages from file contents. Start, finish, cancel, and acknowledgement messages still use JSON. Photo data goes in binary frames. The browser sends a cumulative ACK after writing the data, and the phone uses the amount still unacknowledged to pace the transfer.

Each connection uses one PeerConnection and one DataChannel. Multiple files can be interleaved on logical streams, so there's no need for extra WebRTC connections just to get concurrency. At launch, Watermelon Link used two backup workers by default, with an option to go up to four.

Backups process several resources at once, but lock renewals, cancellations, and cleanup messages still have to get through promptly. The send window reserves room for these small messages so large files can't fill the whole channel.

That was what let the DataChannel reliably carry Watermelon Backup's existing parallel backup workload.

The real device problems came after the architecture diagram

The first prototype showed “Connected” as soon as the other end joined the signaling session. Once, the phone was still on 5G and the browser already looked connected, but there was no usable backup Node in the app. I separated the steps properly after that: open the DataChannel, complete the handshake, get the browser folder ready, register the temporary client, and load the remote index. Only then does the Home screen report a successful connection.

Browser compatibility had its surprises. Edge worked on the same network where Chrome sat in checking. Chrome's Local Network permission turned out to be disabled. I moved the capability and permission checks to the first step and changed the wording from “same Wi-Fi” to “same local network.” The computer can use Ethernet while the phone uses Wi-Fi. An iPhone plugged into Ethernet over USB-C shouldn't be ruled out either.

One concurrency test was more alarming. I backed up four months at once with four workers, and two SQLite manifests wound up with a schema but no records. Several workers were preparing the same parent folder: one created it, another treated “directory already exists” as a conflict, and the manifest never got committed. I made folder creation idempotent and added a regression test for four concurrent months.

Watermelon Link also used to disconnect as soon as the app went into the background. The app's original lifecycle code stopped the task in sceneDidEnterBackground, which conflicted with its PiP handling. Removing that stop let an active transfer continue when PiP was running. It also made me revisit pause and cancel. Stopping a file task should clean up that transfer, not tear down the whole Link.

What about write locks left by a temporary Node?

Watermelon Backup creates a write lock in the backup directory to keep two devices from changing the same repository at once. You can reconnect to an ordinary network Node, but once the Watermelon Link page is closed, that temporary Node is gone.

Say you disconnect and scan again right away. The new connection might find a still-valid lock from the previous session. It shouldn't assume it can take over, but waiting for the lock to expire slows down normal use.

Each Link now has a temporary Node ID. On a clean shutdown, the page stops accepting work, lets queued file operations finish, and records that the session closed safely. When you scan again, the app can use that record to handle the earlier lock.

The browser can't usually do that cleanup if it crashes. In that case, the usual lock timeout applies. It's slower, but I'd rather not guess about the state of the repository.

Web Locks can coordinate tabs from the same website. They can't stop Finder or other processes on your computer, so the page still asks you not to edit the selected folder while a backup is running.

File dates can't be preserved in full

The File System Access API lets Watermelon Link create and write files, but it doesn't let the browser set their original filesystem dates.

Adding a timestamp field to the protocol wouldn't help. Files get the dates assigned when the browser writes them. If filesystem dates are particularly important to you, consider SMB, SFTP, or external storage.

Watermelon Backup puts instructions and Windows and macOS scripts in the backup directory. You can run a script after the backup to read the manifest and set file modification dates from the photos' creation dates.

I see the scripts as optional cleanup. They don't run automatically or restore all the original filesystem timestamps. Without them, the file dates are the dates the browser wrote them. That's one cost of not needing a desktop app.

WebRTC added ten megabytes to the app

Watermelon Link only needs PeerConnection and DataChannel on iOS. Google's WebRTC SDK also includes a lot of code for audio and video capture, codecs, rendering, and RTP.

The full SDK added about 10.2 MB to the executable. Watermelon Backup isn't a large app, so that was hard to ignore.

I set up WatermelonWebRTC separately. It pins the upstream WebRTC source version, uses Google's build tools, and leaves out the audio and video features Watermelon Link doesn't need.

After disabling codecs, audio processing, rendering, and logging, and sorting out Objective-C dependencies and what to link, I got the executable down to 3,385,488 bytes. I was happy with that, but I still asked Codex, “Can we make it any smaller?”

It looked at compiler and linker settings, tried a size profile, -Oz, and ThinLTO, and checked for unused helpers. The arm64 device executable ended up at 2,718,756 bytes.

Not everything helped. ThinLTO at O2 made it bigger. Removing one Objective-C helper built and linked just fine, until it turned out the NSString conversion category was missing at runtime. Other changes saved only a few tens of kilobytes and weren't worth keeping.

The shipped runtime XCFramework archive went from about 16 MB to 3.9 MB. The device executable shrank by about 73%. The source revision, patch hashes, build instructions, and SwiftPM checksum are in the repository README.

Working with Codex on this was more interesting than I'd expected. It suggested what to try, and I built it, checked the size, and ran it on a device before deciding what to keep. What mattered was how each real build turned out.

Looking back

Neither WebRTC nor the File System Access API was new. My job was to connect them to Watermelon Backup's storage layer so the computer wouldn't need a dedicated app and the iPhone could use the backup pipeline it already had.

The browser gets folder access, WebRTC carries data on the local network, and the back end helps establish the connection. BrowserLinkStorageClient passes file operations to the browser. Write locks and ACKs keep the backup in order. Filesystem dates are the part I couldn't keep intact.

What you see is still just a few steps: open the page, choose a folder, scan, and start the backup.

It took a while to get from “Can I back up straight to my computer?” to Watermelon Link. In hindsight, not writing Windows and macOS clients led to a much lighter solution.

You're welcome to discuss the implementation in the GitHub repository.