Overview
TheSyncClient lets you create, open, describe, and synchronize MeshDocuments through a RoomClient. Use it to:
- Create documents (with optional initial JSON) and infer schemas from file extensions.
- Open documents (optionally creating them if missing) and auto-sync changes across participants.
- Describe documents as JSON without opening a sync session.
- Push raw sync payloads when you have pre-encoded changes.
Defining Your Document Structure
To define the structure of a MeshDocument, you start by creating a schema using the MeshSchema class. A schema:- Documents the structure of your MeshDocument for anyone in your organization.
- Ensures agents and users don’t write invalid data to the document.
- Allows agents to automatically generate, manipulate, and validate structured documents.
- Automatically generates LLM-compatible schemas for structured outputs.
- Synchronizes documents across all platforms that MeshAgent supports.
Example Schema
Suppose you have a basic web page structure and want to define a schema for it.Creating a MeshDocument
To create a MeshDocument based on your schema and enable synchronization across different clients, use the MeshAgent runtime:API Methods
Below is an example of how you might use theSyncClient in your code:
SyncClient
The SyncClient class is responsible for coordinating document synchronization through a RoomClient. It automatically listens for local changes on MeshDocument instances and forwards them to the remote server, while also applying remote changes as they are received.
room: RoomClient
An instance ofRoomClient, which handles the underlying communication with the server.
create(path, json=None)
- Parameters:
path– The path where the document will be created on the server.json– (Optional) Initial data or metadata to send along with the creation request.
- Description
Sends a request to create a newMeshDocumentat the given path. The server infers the schema from the document’s extension (for example,.htmluses.schemas/html.json).
describe(path)
- Parameters:
path– The document path to describe.
- Returns
A JSON representation of the document root in a single response. - Description
Fetches the current state of a MeshDocument without opening a sync session. If the document is already open, the in-memory state is returned; otherwise, it is read from storage (schema inferred from the document extension). This is what the storageread_filetool uses to read MeshDocument contents as JSON.- Availability: Supported by the room server; the Python SDK exposes
room.sync.describetoday, while other SDKs may add a convenience wrapper in upcoming releases.
- Availability: Supported by the room server; the Python SDK exposes
open(path, create = true)
- Parameters:
path– The path identifying the document to open.create– (Optional) Whether to create the document if it doesn’t exist. Defaults totrue.
- Returns
AMeshDocumentinstance tied to the specified path. - Description
Sends a request to connect to (and possibly create) the document at the given path. If successful, returns a reference-countedMeshDocumentthat automatically sends local changes to the backend.
close(path)
- Parameters:
path– The path of the document to close.
- Description
Decrements the internal reference count for the document. If it reaches zero, the document is unregistered locally, and a request is sent to the server to disconnect. No further changes will be synced once closed.
sync(path, data)
- Parameters:
path– The path of the document to synchronize.data– AUint8Array(or equivalent) containing serialized changes.
- Description
Immediately sends sync data to the server. This method is useful if you have already encoded your changes and want to bypass the typical queued flow.
Additional Notes
-
Synchronized Changes
Once a document is opened, any local changes you make in the associatedMeshDocumentare automatically queued for sending, thanks tosendChangesToBackend. You rarely need to callsync()manually unless you want to send raw data yourself. -
Reference Counting
TheSyncClientinternally tracks how many times a document is opened via the same path. Callingopenmultiple times for the same path returns the same underlying document. Only when all references are closed (viaclose()) will the client actually disconnect. -
Error Handling
If a request fails, theRoomClientmay throw an error (likeRoomServerException). Wrap calls intry/catch(or use.catch(...)) to handle them gracefully. -
Concurrency and Performance
- For high throughput, ensure that your
RoomClientand server are configured for concurrent operations. - The
SyncClientuses aStreamControllerinternally to manage queued updates, sending them asynchronously to the server.
- For high throughput, ensure that your
-
Extensibility
You can extend theSyncClientfor specialized document operations—such as partial updates, conflict resolution logic, or customized schema handling—by adding new methods or composing it with other classes that also useRoomClient.