Sync Client
How to create an ObjectBox Sync client and connect to an ObjectBox Sync server.
ObjectBox Sync enabled library
The standard ObjectBox (database) library does not include ObjectBox Sync (but may provide Sync API interfaces, to allow compiling).
To get the ObjectBox Sync client library follow the instructions for your programming language:
Follow the Getting Started page instructions. Then change the applied Gradle plugin to the sync variant:
// This automatically adds the native dependency:
apply plugin: "io.objectbox.sync" // instead of "io.objectbox"This will automatically add the Sync variant for your platform.
If needed, e.g. to publish a JVM app that supports multiple platforms or to add Linux ARM support, add the libraries manually:
// Android
implementation("io.objectbox:objectbox-sync-android:$objectboxVersion")
// JVM
implementation("io.objectbox:objectbox-sync-linux:$objectboxVersion")
implementation("io.objectbox:objectbox-sync-macos:$objectboxVersion")
implementation("io.objectbox:objectbox-sync-windows:$objectboxVersion")
// JVM Linux on ARM (not added automatically)
implementation("io.objectbox:objectbox-sync-linux-arm64:$objectboxVersion")
implementation("io.objectbox:objectbox-sync-linux-armv7:$objectboxVersion")We may distribute ObjectBox Sync for Swift in our Cocoapods staging repository (details will be provided by the ObjectBox team). In that case, these are some typical lines to put in your Podfile (please check the version, there might be a newer one available):
target 'MyCoolSyncProject' do
use_frameworks!
pod 'ObjectBox', '4.4.0-sync'
end
See the Getting Started instructions for Flutter or Dart and note the different instructions for Sync (different Flutter library, increasing Android minSdkVersion, install script parameter).
Or use CMake's FetchContent to get ObjectBox headers and library ready to use in your project:
Please reach out to the ObjectBox team.
Now it is time to verify the setup using a flag telling if Sync is available; for example, simply log the result:
Enable your Objects for ObjectBox Sync
ObjectBox Sync allows you to define which objects are synced and which are not. This is done at an object type level (a "class" in many programming languages). By default, an object (type) is local only: objects are kept in the database on the local device and do not get synced to other devices.
To enable sync for an object type, you add a "sync" annotation to the type definition. This is typically the entity source file, or, if you are using ObjectBox Generator, the FlatBuffers schema file:
Once the sync annotation is set on the intended types, you need to rebuild (e.g. Java/Kotlin) or trigger the ObjectBox generator (e.g. C and C++). This activates a "sync flag" in the metamodel (e.g. the model JSON file is updated).
If you already have a non-synced type that you now want to sync (see also the info box above), these are the typical options you have:
If you are still in development, add the sync annotation and wipe your database(s) to start fresh with that new data model.
"Replace" the entity type using a new UID (check schema changes docs for the ObjectBox binding you are using). You can keep the type name; to ObjectBox it will be a different type as the UID is different. This will delete all existing data in that type.
Have a second, synced, object type and migrate your data in your code following your rules.
Start the Sync Client
Create a Sync client for your Store and start it. It connects to a given sync server URL using some form of credentials to authenticate with the server. A minimal setup can look like this:
Sync client is started by calling start() or buildAndStart(). It will then try to connect to the server, authenticate and start syncing. Read below for more configuration options you can use before starting the connection.
Once the client is logged in, the server will push any changes it has missed. The server will also push any future changes while the client remains connected. This sync updates behavior can be configured.
All of this happens asynchronously. To observe these events (log in, sync completed, …) read below on how to configure an event listener.
The client will now also push changes to the server for each Store transaction.
Should the client get disconnected, e.g. due to internet connection issues, it will automatically try to reconnect using an exponential back-off. Once the connection succeeds, data synchronization resumes.
Always close the client before closing the store. Closing the store with a still running sync client results in undefined behavior (e.g. crashes). Keep in mind that it typically is fine to leave the sync client and store open; once the application exits, they will be automatically closed properly.
Sync filter client variables
Sync clients may provide variables for sync filters, see sync filters and specifically the the section on client variables for general information.
The client APIs to add sync filter variables take name/value pairs (both strings) and look like this:
Supplying multiple values for IN conditions
When you use the IN operator in a sync filter, you want to provide a multiple values to a variable. This can be done by providing a string that contains a comma-separated list of values.
Let's suppose we have a sync filter "fruit IN $client.fruits"; then we can provide the values like this:
Note: future versions of the client APIs will also take a list of values. This will also take care of escaping special characters for string values, as mentioned in the sync filter documentation.
Drop-off, send-only clients
For some use cases, a client should only report data and thus only send updates without ever receiving any data. We call those "drop-off clients". Technically, from an API perspective, these clients do not request updates from the server. Because requesting updates is the default, the sync client API has to be configured to do "manual" updates to actually disable updates from the server. This configuration has to happen before the client starts.
Secure Connection
When using wss as the protocol in the server URL a TLS encrypted connection is established. Use ws instead to turn off transport encryption (insecure, not recommended; e.g. only use for testing).
Authentication options
There are currently multiple supported options for authenticating clients with a Sync server.
JWT authentication
Clients can be authenticated using tokens in JWT (JSON web token) format. The general process is outlined in the server-side JWT documentation. Your client application typically will use a JWT authentication provider SDK to get a token in JWT format. This token is then set as a credential using the ObjectBox Sync client API:
Shared secret
This can be any pre-shared secret string or a byte sequence.
Google Sign-In
The ObjectBox Sync server supports authenticating users using their Google account. This assumes Google Sign-In is integrated into your app and it has obtained the user's ID token.
Coming soon!
Coming soon!
No authentication (unsecure)
Never use this option in an app shipped to customers. It is inherently insecure and allows anyone to connect to the sync server.
For development and testing, it is often easier to just have no authentication at all to quickly get things up and running.
Coming soon!
Manually start
In the Java and Kotlin example above, the sync client automatically connects to the server and starts to sync. It is also possible to just build the client and then start to sync once your code is ready to.
Note that a started sync client can not be started again. Stop and close an existing one and build a new one instead.
Listening to events
The sync client supports listening to various events, e.g. if authentication has failed or if the client was disconnected from the server. This enables other components of an app, like the user interface, to react accordingly.
It's possible to set one or more specific listeners that observe some events, or a general listener that observes all events. When building a Sync client use:
loginListener(listener)to observe login events.completedListener(listener)to observe when synchronization has completed.connectionListener(listener)to observe connection events.listener(listener)to observe all of the above events. UseAbstractSyncListenerand only override methods of interest to simplify your listener implementation.
See the description of each listener class and its methods for details.
Note that listeners can also be set or removed at any later point using SyncClient.setSyncListener(listener) and related methods.
It's possible to set one or more specific listeners that observe some events, or a general listener that observes all events. When building a Sync client use:
loginListener(listener)to observe login events.completedListener(listener)to observe when synchronization has completed.connectionListener(listener)to observe connection events.listener(listener)to observe all of the above events. UseAbstractSyncListenerand only override methods of interest to simplify your listener implementation.
See the description of each listener class and its methods for details.
Note that listeners can also be set or removed at any later point using SyncClient.setSyncListener(listener) and related methods.
It's possible to set one or more specific listeners that observe some events, or a general listener that observes all events. The SyncClient protocol offers the following properties to attach listeners:
loginListenerto observe login events.completedListenerto observe when synchronization has completed.connectionListenerto observe connection events.listenerto observe all of the above events.
There is a protocol for each listener type. Note that listeners can also be set or removed at any later point by setting the listener property to nil.
By implementing a listener protocol and setting the matching property in SyncClient, you are called back. Let's have a look at the available listener protocols for details:
It's possible to listen to sync-related events on the client. Use the following SyncClient getters to connect to a stream:
Stream<SyncLoginEvent> get loginEvents- such as logged-in, credentials-rejected.Stream<void> get completionEventsto observe when synchronization has completed.Stream<SyncConnectionEvent> get connectionEventsto observe connection events.
Note that these streams don't buffer events so unless you're subscribed, no events are collected. Additionally, don't forget to cancel the subscription when you don't care about the information anymore, to free up resources.
Advanced
Listening to incoming data changes
For advanced use cases, it might be useful to know exactly which objects have changed during an incoming sync update. This is typically not necessary, as observing a box or a query may be easier.
On each sync update received on the client, the listener is called with an array of "Sync Change" objects, one for each affected entity type. It includes a list of affected object IDs - the ones that were put or removed in the incoming update.
Use changeListener(changeListener) when building the client and pass a SyncChangeListener to receive detailed information for each sync update. Or set or remove it at any later point using SyncClient.setSyncChangeListener(changeListener).
Use changeListener(changeListener) when building the client and pass a SyncChangeListener to receive detailed information for each sync update. Or set or remove it at any later point using SyncClient.setSyncChangeListener(changeListener).
Coming soon!
Use Stream<List<SyncChange>> get changeEvents on the SyncClient to receive detailed information for each sync update. Make sure to cancel the subscription when you don't need the information anymore to clear up resources.
Checking for outgoing changes
Sometimes you want to know if there are any ("outgoing") changes on the local device that are not yet synchronized to the server. This is helpful to when you want to show the sync status in the user interface, or trigger some logic. Technically, ObjectBox uses a message queue here and there's an API that gives you number of outgoing messages. If this number reaches zero, it means that all changes done on this device have been synced (sent) to the server. It's fine to call this API periodically, e.g. every second, if you want to know the current status.
Listeners concurrency
Some events may be issued in parallel, from multiple background threads. To help you understand when and how you need to take care of concurrency (e.g. use mutex/atomic variables), we've grouped the sync listeners to these two groups:
State listeners - listening to login success/failure, connection status, sync complete.
Data change listener - listening to incoming data changes.
There can be only one event executed at any single moment from a listener in a single group. You can imagine this as if there were two parallel threads, one could only issue "state" events, the other only "data change" events.
Controlling sync updates behavior
By default, after the Sync client is logged in, its database is updated from the server and the client will automatically subscribe for any future changes. For advanced use cases, like unit testing, it is possible to control when the client receives data updates from the server.
To change the default behavior, configure the "Request Updates Mode" before starting the client connection. Three modes are available:
automatic (default): receives updates on login and subscribes for future updates.
automatic, but no pushes: receives updates on login but doesn't subscribe for future updates.
manual: no automatic updates on login or on any updates in the future.
When using one of the non-default modes, synchronization can be controlled after login during application runtime by requesting and cancelling updates using the client:
Coming soon!
Last updated
Was this helpful?