Repository navigation
Sync Service
English | 中文
The server behind Sync server (database) in cloud sync, deployed on your own Cloudflare account (Workers and a D1 database). The code is in the sync_server/ folder of the repository.
Since 8.2.0, S3, WebDAV and the sync server follow the same rules: one record per item, the later change wins, and deletions (unfavoriting, removing history and so on) reach other devices. They differ in where the data is: the sync server keeps it in a D1 database and each sync transfers only what other devices wrote since the last one; S3 and WebDAV keep it in records/v1/ in the bucket or folder.
Upgrading from 8.1.1 or earlier does not need the server redeployed, but every device needs upgrading, see Cloud sync.
- Node.js;
- a Cloudflare account (the free plan is enough).
Run these commands in the sync_server/ folder.
-
Install the dependencies:
npm install
-
Log in to Cloudflare (a browser opens):
npx wrangler login
-
Create the D1 database and note the
database_idit prints:npx wrangler d1 create gallery-sync
-
Copy the configuration and put the
database_idin it (wrangler.tomlis not committed):cp wrangler.example.toml wrangler.toml
-
Set the token. It is stored as an encrypted variable of the Worker, not in any file; it is what you enter in the app.
openssl rand -hex 32makes one:npx wrangler secret put SYNC_TOKEN
-
Deploy. The
https://gallery-sync.<subdomain>.workers.devit prints is the server address:npx wrangler deploy
The Worker creates its tables on the first request; there is nothing to set up by hand.
-
Setting→Advanced→Cloud Sync Settings, turn onEnable Sync. - Set
Sync ProvidertoSync server (database). - Fill in
Server address(https://is added when missing) andToken. - Tap
Test Connection.
Every device chooses the sync server and enters the same address and token. The provider is each device's own choice and is not changed by other devices.
The free allowances of D1 and Workers are enough for one person. The largest use is the first sync of the first device: one row for each history entry, each reading-progress entry and each favorite. After that, each sync writes only what changed.
| What you see | Cause and what to do |
|---|---|
| Sync Failed: Sync server (401): Unauthorized | The token differs from the Worker's SYNC_TOKEN. |
| The Worker answers SYNC_TOKEN is not set for this Worker | Step 5 was skipped, or the deployment went to another Worker. |
| Sync Failed: The sync server address is not set | The server address is empty. |
workers.dev cannot be reached |
Some networks block workers.dev. Use a proxy in the app's network settings (sync requests follow the app's proxy), or bind your own domain to the Worker in Cloudflare and enter that domain in the app. |
| Sync Failed: Sync server: protocol N is not supported | Server and app speak different protocol versions; update both. |
Protocol 1; every request carries Authorization: Bearer <token>:
-
GET /v1/info:{"protocol": 1, "database": "<database id>", "seq": <highest sequence>} -
POST /v1/push: an array of records, each withkind,key,value,updatedAt,device,deleted; for the same(kind, key)the larger(updatedAt, device)wins. -
GET /v1/pull?since=<sequence>&device=<device>&limit=<count>: the records other devices wrote after that sequence.
Any server that speaks the same protocol can serve as the sync service.