Skip to content

Instantly share code, notes, and snippets.

@cfloisand
Created December 2, 2020 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfloisand/e9dcfa54e4b7a50cd4b3e44a4e7477e5 to your computer and use it in GitHub Desktop.
Save cfloisand/e9dcfa54e4b7a50cd4b3e44a4e7477e5 to your computer and use it in GitHub Desktop.
sync config
// One-shot download configuration
try {
gatewayUrl = "wss://..."
Endpoint gatewayEndpoint = new URLEndpoint(new URI(gatewayUrl));
pullConfig = new ReplicatorConfiguration(db, gatewayEndpoint);
} catch (Exception e) {
//...
}
String cookie = ...;
String channel = ...;
String cookieName = ...;
String cookieValue = ...;
pullConfig.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PULL);
pullConfig.setContinuous(false);
pullConfig.setChannels(Arrays.asList(channel));
SessionAuthenticator authenticator = new SessionAuthenticator(cookieValue, cookieName);
pullConfig.setAuthenticator(authenticator);
replicator = new Replicator(pullConfig);
replicatorListener = new ReplicatorListener();
replicatorListener.addDownloadListener(() -> {
startSync(); //<< One-shot download is done, so start sync mode
});
replicatorToken = replicator.addChangeListener(replicatorListener);
replicator.start();
// Starting sync mode
private void startSync() {
replicator.stop();
replicator.removeChangeListener(replicatorToken);
ReplicatorConfiguration syncConfig = new ReplicatorConfiguration(replicator.getConfig());
syncConfig.setContinuous(true);
syncConfig.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH_AND_PULL);
replicator = new Replicator(syncConfig);
replicatorToken = replicator.addChangeListener(replicatorListener);
replicator.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment