Embedded Sync Server
How to set up an ObjectBox Sync server that is embedded in your application.
This page refers to the embedded sync server, not the standalone sync server. Usually, you want to use the standalone server.
Embedded servers are currently available for Java, C and C++. Let us know if you need another platform.
Use Sync.server(boxStore, url, authenticatorCredentials) to start a Sync server using a boxStore . The server binds to the address and port given in url.
When using wss as the protocol of the url a TLS encrypted connection is established. Use certificatePath(path) to supply a path to a certificate in PEM format. Use ws instead to turn off transport encryption (insecure, only use for testing!).
authenticatorCredentials are required to authenticate clients. It is possible to add more than one set of allowed credentials.
Java
C++
1
SyncServer syncServer = Sync.server(
2
boxStore,
3
"wss://0.0.0.0:9999" /* Use ws for unencrypted traffic. */,
4
SyncCredentials.sharedSecret("<secret>")
5
) // Builder...
6
.certificatePath(certPath) // Server PEM certificate
7
.authenticatorCredentials(cred2) // Additional credentials
8
.buildAndStart();
Copied!
1
// Developer options; use wss and real credentials in production...
2
obx::SyncServer server(std::move(storeOptions), "ws://0.0.0.0:9999");
3
server.setCredentials(obx::SyncCredentials::none());
4
server.start();
5
obx::Store& store = server.store();
Copied!
During buildAndStart() the server will start and become ready to accept connections from clients. Read below for more configuration options you can use before starting the connection.

Client authentication

These are the currently supported options to authenticate clients:

Shared secret

1
SyncCredentials credentials = SyncCredentials.sharedSecret("<secret>");
Copied!
This can be any pre-shared secret string or a byte sequence.

Google Sign-In

Not available, yet.

No authentication (insecure)

Never use this option to serve an app shipped to customers. It is inherently insecure and allows anyone to access the sync server.
1
SyncCredentials credentials = SyncCredentials.none();
Copied!
For development and testing it is often easier to just have no authentication at all to quickly get things up and running.

Manually start

Using the example above, the server automatically binds to the given address and port and starts listening for clients. It is also possible to just build the server and then start it once your code is ready to.
1
// Just build the server.
2
SyncServer syncServer = Sync.server(...).build();
3
4
// Start now.
5
syncServer.start();
Copied!
Note that a started server can not be started again. Stop and close an existing server and build a new one instead.

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.
To listen to these incoming data changes:
1
SyncChangeListener changeListener = syncChanges -> {
2
for (SyncChange syncChange : syncChanges) {
3
// This is equal to Example_.__ENTITY_ID.
4
long entityId = syncChange.getEntityTypeId();
5
// The @Id values of changed and removed entities.
6
long[] changed = syncChange.getChangedIds();
7
long[] removed = syncChange.getRemovedIds();
8
}
9
};
10
// Set the listener when building the server.
11
syncBuilder.changeListener(changeListener);
12
// Or set the listener later.
13
syncServer.setSyncChangeListener(changeListener);
14
15
// Calling again replaces an existing listener.
16
syncServer.setSyncChangeListener(changeListener);
17
// Remove an existing listener.
18
syncServer.setSyncChangeListener(null);
Copied!
On each sync update received on the server, 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.

Adding peer servers

Before using peer servers, please reach out to the ObjectBox team.
It is possible to have multiple sync servers for redundancy and load balancing where one or more secondary servers connect as special clients to a primary server.
To add the primary server when building a secondary server:
1
syncBuilder.peer(primaryServerUrl, SyncCredentials.sharedSecret("<secret>"));
Copied!
Last modified 5mo ago