This file is shared by mobile-sync-android and mobile-sync-ios.
Platform-specific code (Kotlin/Swift) lives in each skill's own SKILL.md.
Mobile Device
└── Couchbase Lite (embedded JSON database)
│ WebSocket (wss://)
▼
Sync Gateway / Capella App Services
│
▼
Couchbase Server / Capella
Couchbase Lite syncs over WebSocket. The Sync Gateway (or Capella App Services) mediates access control via the Sync Function. The mobile app always reads and writes locally — replication happens in the background.
The Sync Function runs server-side in JavaScript. It is not platform-specific.
function sync(doc, oldDoc) {
// Route to user's private channel
channel("user." + doc.userId);
// Require the user to own the document
requireUser(doc.userId);
}Key Sync Function APIs:
channel(name)— assign document to a channelrequireUser(username)— reject write unless the user matchesrequireRole(role)— reject write unless the user has the rolerequireAccess(channel)— reject write unless the user has channel accessaccess(username, channel)— grant a user access to a channel
The mobile app always reads and writes to the local Couchbase Lite database. Replication runs in the background — the app never waits for the network.
Design principle: treat the local database as the source of truth. Sync is an implementation detail, not a dependency.
| Symptom | Likely cause | Fix |
|---|---|---|
| 401 on replication | Wrong credentials or expired session | Re-authenticate; check Sync Gateway logs |
| Documents not syncing | Channel mismatch | Verify Sync Function assigns correct channels |
| Replication stops after idle | Network timeout | Enable heartbeat in ReplicatorConfiguration |
| Conflict on every write | Two devices writing same doc key | Use per-user document keys; see conflict-resolution skill |
| Large initial sync | No delta sync | Enable delta sync in Sync Gateway config |
| Feature | Capella App Services | Self-hosted Sync Gateway |
|---|---|---|
| Setup | Managed — no install | Manual install + config |
| Sync Function | Supported | Supported |
| Delta sync | Enabled by default | Requires config |
| TLS | Always on | Configurable |
| Admin API | Capella Management API | REST on port 4985 |