Start Sync
Ensure the sync process remains active throughout your app’s lifecycle by starting the sync process (startSync) in the top-most scope of your code.
To initiate the sync process:
Subscriptions
With a subscription query, you can declaratively describe the data that you want a particular device to receive. When a query is subscribed, Ditto continuously syncs relevant documents that match the query to the local device. The subscription query represents “what you want other peers to send you.” It is not a query that you run against the local database, but rather a query that you run against the remote peers to receive updates about documents that match the query.For operational best practices — scoping subscriptions, keeping them stable, and avoiding patterns that degrade sync performance — see Sync Handling.
Creating Subscriptions
The following snippet demonstrates how to establish a subscription to sync updates to documents in thecars collection with a field of color set to the value
blue:
:argument syntax
in DQL:
Unsupported Query Syntax
A subscription query describes which documents sync to the peer, not the order they are displayed in. SomeSELECT syntax is therefore rejected when you
register a subscription, even though the same query runs fine through execute
or registerObserver:
LIMITandORDER BY— registering a subscription whose query contains either clause fails with<dql> Unsupported feature: Limit or Order by. Remove the clauses from the subscription and sort or page the results at query time instead, in anexecuteorregisterObserverquery. See Stateful Subscription Performance Guidelines for background on why these clauses are restricted.- Joins — a subscription query that joins collections fails with an
unsupported query error. Subscribe to each collection separately and perform
the join in an
executeorregisterObserverquery. See Joins — Restrictions.
Retrieving Subscriptions
Retrieve active sync subscriptions by calling thesubscriptions method on the ditto.sync namespace:
Canceling Subscriptions
To cancel a subscription, callcancel on its subscription object you
instantiated when setting up your subscription.
isCancelled field on the
subscription object:
Stopping Sync
To stop the sync process, call thestopSync function. Once called, you will be disconnect from any peers and all active sync subscriptions will be paused.