Wai Hon's Blog

Use Syncthing with WebDAV to Sync Everywhere

2022-11-04 #org-mode #emacs #productivity

I have been using Syncthing with Caddy’s WebDAV to synchronize my Org Mode files across computers and mobiles for few weeks. I am very satisfied with it and writing this post about the setup.

Why not just Syncthing?

My previous setup use solely Syncthing to synchronize them across computers and mobiles. While Syncthing works great on computers, it is problematic on mobiles because mobile can switch networks, have a limited battery, and have limited background sync capability (especially on iOS). Out-dated files or conflicts happen more often to me when I use a mobile device.

I noticed some Org Mode mobile apps (like “beorg” and “Orgzly”) support WebDAV sync. Hence, I tried to add a WebDAV entry point for my org files and see how it goes.

Overview

I run Syncthing and WebDAV both watching the same org directory on a Raspberry Pi 3B. Computers and mobiles are connected with Syncthing and WebDAV respectively.

WebDAV Setup

I first tried Nextcloud but chose Caddy eventually to set up WebDAV.

Attempt #1: Nextcloud

My first attempt was using Nextcloud’s WebDAV, after searching “Syncthing WebDAV” and found this story.

I added the Syncthing’s local folder to Nextcloud via external storage so it could be accessed from WebDAV. It worked. It proved that Syncthing with WebDAV is a viable solution.

However, Nextcloud is too heavy for my Raspberry Pi 3. The latency is high enough to degrade the mobile experience. This could be a good solution for existing Nextcloud users with a faster hosting machine.

Attempt #2: Caddy WebDAV

Since my Raspberry Pi is already using Caddy as the web server, why not give Caddy’s WebDAV a shot? Caddy is written in Go and should have better performance.

The WebDAV module was not built-in and I need to built a new Caddy binary with xcaddy. Then, I updated the systemd service to run as the same user as syncthing to get the file permission right.

It worked and indeed ran so much faster. I am satisfied!

Android and iOS Setup

I use beorg on iOS and Orgzly on Android. Both of them support “WebDAV” with “Auto-sync”. “Auto-sync” performs a sync when a note is changed or when the app is resumed. This greatly reduces the chance of conflict, when compared with Syncthing.

In addition, I found beorg also comes with a LISP runtime (BiwaScheme) and allows users to customize the app in a way similar to Emacs. The customization is limited at the moment but it has the potential!

Syncthing Setup

I use most of the default configuration. My only tweak is to reduce fsWatcherDelayS from 10s to 1s in all instances. This makes the synchronization happens more instantly and seamlessly between devices. For example, when I make a change on the phone, it updates the Raspberry Pi and synchronizes across other computers in a second.

Conclusion

WebDAV is simple and cheap to host. It is secure to use over the Internet when pairing with BasicAuth and HTTPS (both supported by Caddy). It is not a perfect synchronization tool but good enough to fill the mobile gap.

Finally, this is the corresponding WebDAV snippet from my Caddyfile.

my.webdav.host {

    basicauth * {
        <user> <hash>
    }

    root * /path/to/webdav

    @get method GET
    route {
        file_server @get {
            hide .git .gitignore .stfolder .stversions
            browse
        }

        webdav
    }
}