Syncing Data

Introduction

Syncbase's sync protocol is peer-to-peer whereas most other sync systems require a centralized server. We believe that despite internet connectivity becoming more and more prevalent, there will always be times when an internet connection is not available. You should be able to sync with your peer, with very low latency, when you are physically close. For example, you shouldn't need an internet connection to set the temperature on your thermostat. Syncbase uses the cloud as another, very durable peer, but the cloud is not required for any two peers to interact. Because the cloud is not in the critical path for synchronization, apps can use Syncbase as for asynchronous, relatively low latency communication.

Peer-to-peer sync, however, introduces problems not present in client-server sync:

Syncbase internally handles both of these issues by providing:

Using Syncgroups

A syncgroup is a set of of collections that are synchronized amongst a set of users (and with the cloud, if available).

By default, creating a collection creates an associated syncgroup, initially synced amongst the creator’s devices but other users can also be added to this syncgroup to allow sharing.

Sharing Collections

Sharing collections involves inviting other users to join a collection's syncgroup. Upon inviting a user, the invitee receives an invite event. When an invite is accepted, the inviter's syncgroup will be joined and shared data will start syncing. When inviting a user, an access level can be specified:

On the inviter side, we just need to invite a user to join the collection's syncgroup:

cat - <<EOF >> $FILE
Collection collectionToShare = db.createCollection();

collectionToShare.getSyncgroup().inviteUser(userToInvite, AccessList.AccessLevel.READ);
EOF

On the invitee side, we need to handle invite requests by registering a handler:

cat - <<EOF >> $FILE
db.addSyncgroupInviteHandler(new Database.SyncgroupInviteHandler() {
  @Override
  public void onInvite(SyncgroupInvite invite) {
    // Prompt the user if desired then accept or reject the invite.
    db.acceptSyncgroupInvite(invite, new Database.AcceptSyncgroupInviteCallback() {
      @Override
      public void onSuccess(Syncgroup sg) {
        // Accepting invitation was successful.
      }

      @Override
      public void onFailure(Throwable t) {
        // Accepting invitation was unsuccessful.
      }
    });
  }

  @Override
  public void onError(Throwable t) {
    // Invite handler error.
  }
});
EOF

Tip

db.removeAllSyncgroupInviteHandlers() can be used in activity's onDestroy to remove all registered invite handlers.

When an invitation is accepted, Syncbase automatically joins the inviter's syncgroup and the associated collection and its data will start syncing into the invitee's database. As the collection syncs, data will be surfaced through the Watch API. See Data Flow guide for details on how to model your app's data flow.

Unsharing Collections

Ejecting a user from a collection's syncgroup will unshare the collection. If the target user has not accepted the invitation yet, the invite will simply disappear. Otherwise, the shared collection on target user's database will become read-only and will no longer sync and receive updates.

cat - <<EOF >> $FILE
Collection sharedCollection = db.createCollection();

sharedCollection.getSyncgroup().ejectUser(userToRemove);
EOF

Updating Access Level

Simply re-inviting an already invited user with a different access level will update their access without triggering a new invitation.

Listing All Syncgroups

db.getSyncgroups() can be used to list all syncgroups. This list includes pre-created syncgroups for collections and other syncgroups created or joined.

cat - <<EOF >> $FILE
Iterator<Syncgroup> allSyncgroups = db.getSyncgroups();
while(allSyncgroups.hasNext()) {
    Syncgroup sg = allSyncgroups.next();
}
EOF

Summary