Connection states and errors
The states a client document moves through, the errors it reports, and what to show the user for each.
A client document keeps working through dropped connections, slow servers and rejected edits. This page lists the states it can be in, the callbacks and errors it reports, and what each one asks of your application.
State you can read
| Method | Meaning |
|---|---|
isConnected() | A session is open |
isReady() | The document's state has arrived; true from then on |
exists() | Whether the document exists; undefined until it is ready |
isReadOnly() | Writes throw ReadOnlyError |
hasPendingChanges() | Some local edits are not answered by the server yet |
getPendingCount() | How many local edits are unanswered |
getSyncError() | Why local edits are not reaching the server, if they are not |
getSchemaMismatch() | How this client's schema differs from the document's, if it does |
Each has a matching listener in document.subscribe: onConnectionChange,
onReady, onExistenceChange, onStateChange, onRejected, onSyncError
and onSchemaMismatch. The same events are available as options of
createSyncClientDocument.
A typical status indicator needs three of them:
const status = () => {
if (!document.isReady()) return "Loading";
if (!document.isConnected()) return "Offline";
if (document.hasPendingChanges()) return "Saving";
return "Saved";
};Offline editing
A disconnected document stays usable. Edits made while offline apply locally and wait in the pending queue; when the session is back, the client catches up with the server first and then sends them, one at a time and in order. Nothing needs to be done for this.
By default the queue lives in memory, so closing the page loses edits the server has not answered. See Pending edits to keep them across reloads.
Read-only documents
isReadOnly() is true, and writes throw ReadOnlyError, in two cases.
error.reason tells them apart:
reason | Cause |
|---|---|
permission | The session's credential grants read only |
schema_mismatch | This client was built for another schema than the document has; see Schema versions |
Check isReadOnly() to disable editing controls instead of letting writes
throw. Edits already queued stay queued while the document is read-only.
Rejected edits
onRejection(transactionId, reason, code, engineCode) is called once for
every local edit that will never apply. Its effect has already been removed
from the local value when the callback runs. code is one of invalid,
conflict, not_found, forbidden and internal; engineCode is the
server's own error code, when the server gave one.
reason, code or engineCode | What happened |
|---|---|
engineCode validation_failed, code invalid | Combined with other users' edits, the result would violate the collection's schema |
reason "migration_blocked", code invalid | A migration of the collection is being applied, and the result would fail it |
reason "migration_version", code conflict | The edit was made on another schema version than the document now has |
engineCode incarnation_mismatch, code conflict | The document was deleted (and maybe created again) since the edit was made |
engineCode already_exists, code conflict | A creation lost to another client's |
code forbidden | The server refused the write: no write permission, or the database is read-only |
code not_found | The document no longer exists |
reason "expired" | A persisted edit was older than the server's retry horizon (30 days by default) |
reason "clock_ahead" | A persisted edit's id was made by a clock far ahead of the server's, and was never applied |
reason "unknown" | An edit was sent, but no answer can be had any more: it may or may not have applied |
The reasons are exported from @voidhash/mimic/sync as constants:
MIGRATION_BLOCKED_REASON, CLOCK_AHEAD_REASON and OUTCOME_UNKNOWN_REASON
("migration_version" and "expired" keep the names they always had).
A rejection means the user saw an edit that is now gone. Tell them. An edit
the server evaluated but that changed nothing (another user had already made
the same change) is not a rejection: onRejection is not called for it, and
the onRejected listener receives it with the reason "unchanged", so an
undo history can forget it.
Sync errors
onSyncError(error) is called when local edits stop reaching the server, or
when syncing stops. getSyncError() returns the error until it no longer
applies.
MimicSyncError. The session reported an error, with the server's code
as engineCode. When terminal is true, the document stopped syncing and
does not reconnect; pending edits are kept, and persisted ones survive for a
later instance. The terminal codes are those of
Realtime sessions:
unauthenticated after two refused credentials, permission_denied,
not_found, migration_failed, schema_mismatch, unsupported_version,
data_corruption and invalid_argument. Otherwise the client keeps trying.
import { MimicSyncError } from "@voidhash/mimic/sync";
document.subscribe({
onSyncError: (error) => {
if (error instanceof MimicSyncError && error.terminal) {
if (error.engineCode === "unauthenticated") signInAgain();
else showUnavailableNotice(error.engineCode);
}
},
});SnapshotValidationError. A state from the server does not satisfy this
client's schema, which usually means the client is outdated. The client
stops sending, and local edits stay queued until a valid state arrives. Ask
the user to reload.
TransactionRetryError. The server failed the same edit five times in a
row without an answer (unavailable, resource_exhausted or internal).
The client keeps retrying it with backoff, from half a second up to 30
seconds, and later edits wait behind it until it is answered. Show that
saving is delayed. The error carries transactionId, attempts and
reason.
The connection
The client reconnects by itself after any loss, with backoff, and needs no
handling. onConnectionChange(connected) tells you when a session opens or
closes, for an offline indicator. disconnect() closes the session until the
next connect().
When the server sends the document's state instead of the changes the client
expected (its history was released, or replaced by a restore), the client
calls onReplaced(reason). Pending edits stay queued and are evaluated on
the new state. You may want to tell the user that what they saw before was
replaced.
Error classes
All errors the client throws or reports extend MimicClientError, which
@voidhash/mimic/sync exports with ReadOnlyError, MimicSyncError,
TransactionRetryError, PendingStoreError and DocumentNotFoundError.
InvalidStateError and SnapshotValidationError are not exported from it
yet: tell them apart by their name.
| Class | When |
|---|---|
ReadOnlyError | A write was attempted on a read-only document |
InvalidStateError | The document or a draft was used in a state that does not allow it |
MimicSyncError | The session reported an error; terminal when syncing stopped for good |
SnapshotValidationError | A state from the server does not satisfy the client's schema |
TransactionRetryError | The server keeps failing an edit without an answer |
PendingStoreError | Pending edits could not be read from or written to durable storage |
Recovering by hand
resync() asks the server for the document's current state and resolves
when it has arrived. It rejects when the document is disconnected. The
client resynchronizes on its own whenever it needs to, so this is rarely
necessary.