Synchronize client data
Delivery is still gated on the account-deletion lifecycle and production smoke tests. The public adapter is off by default. See delivery status before enabling a consumer.
Mocco stores bounded JSON records for one signed-in app user. Your application keeps its local database, changes made offline, application schemas and conflict decisions. Rendering settings and using a directly connected service must not wait for Mocco.
Set up a namespace
A workspace owner or admin enables Data sync for the project, registers an immutable namespace such as settings-v1, and enables it in the intended identity environment. Use separate production and development identity environments. A publishable key needs both identity:auth and data:sync, bound to that same environment. Only that environment's web origins may use it from a browser.
The matching operator tools are mocco_data_sync_namespaces_list, mocco_data_sync_namespace_create, mocco_data_sync_namespace_set_enabled and mocco_data_sync_usage_list. Reads are paged and expose configuration/counts only. Writes require an administrator's token, data-sync:write, workspace opt-in and confirmation.
For self-hosting, configure end-user identity and a separate random DATA_SYNC_CURSOR_SECRET of at least 32 characters. Keep it unset until lifecycle checks are complete. Use HTTPS; the SDK permits plain HTTP only on loopback for development. Redirects are refused before credentials can follow them.
Bind the SDK to one account
Install from the fi-workers registry:
npm install @mocco/sdk-core --@mocco:registry=https://packages.mocco.club/api/packages/fi-workers/npm/After signing in with the identity SDK, use its atomic session snapshot:
import { createDataSyncClient, DataSyncOperations } from '@mocco/sdk-core/data-sync';
const signedIn = await identity.emailVerify(challengeId, code);
const sync = createDataSyncClient({
publishableKey,
userId: signedIn.user.id,
session: identity.session,
});
const config = await sync.config();
const snapshot = await sync.snapshot('settings-v1');Create a different client for a different account. identity.session() refreshes when needed and returns a coherent { accessToken, userId } or null; it refuses a snapshot replaced while being read. Do not combine an independently read user ID with a separately fetched token. An in-flight operation keeps its captured identity through retries; subsequent calls after an account switch refuse. Your application must keep each account's pending writes and received data in that account's local repository.
The JavaScript runtime needs fetch, URL, URLSearchParams and TextEncoder. The identity SDK additionally uses WebCrypto for browser sign-in; native hosts supply their platform adapters.
Keep local changes durable
Save a local edit and its outbox entry in one local transaction, then update the UI immediately. A pending mutation has an immutable UUID, body and owner. Reuse its original ID and body after a timeout; changing input under the same ID conflicts.
const receipt = await sync.mutate('settings-v1', {
mutationId: pending.id,
generation: pending.generation,
observedCursor: pending.observedCursor,
recordId: 'preferences:appearance',
operation: DataSyncOperations.Upsert,
expectedRevision: pending.expectedRevision,
schemaVersion: 1,
payload: { theme: 'dark' },
});Mark the matching outbox entry acknowledged after storing its receipt. A receipt's cursor does not prove earlier remote changes have been applied locally. Advance a durable pull cursor only in the same transaction that applies a snapshot or changes page. Preserve payloads with application schema versions your current app cannot apply.
Deletion needs the positive revision you observed; a create-only null revision cannot delete a record or resurrect a tombstone. A restore must explicitly address the observed tombstone revision.
Handle recovery explicitly
MoccoDataSyncError.code distinguishes sync_conflict, sync_snapshot_required, sync_invalid_cursor and sync_quota_exceeded. A conflict exposes the owned current record or null through an explicit getter. Its payload is omitted from ordinary error serialization.
On conflict, reconcile the local intent with that record and create a new mutation for a new decision. On a stale or invalidated cursor, take a fresh snapshot, preserve pending local intent and reconcile before replacing request metadata. Never silently rewrite an uncertain mutation's body under its old ID.
Capacity refusals leave local changes intact. Initial bounds are returned by config(); retained changes/receipts expire after 30 days. Deletes and identical retries remain possible when the history budget is full. A 429 response provides Retry-After. Network failure or managed sync being disabled must not disable local use.
Only application-controlled preferences and connection metadata belong in these records. Provider passwords, API keys, tokens and device private keys stay outside ordinary sync. This delivery does not claim end-to-end encryption; disclose managed metadata storage to your users.