Couchbase Sync Gateway is a key Internet facing component of Couchbase Mobile stack that enables secure data syncronization across Couchbase Lite enabled clients. Couchbase Mobile 2.0 introduced a brand new websockets based replication protocol for data syncronization that is more efficient than it’s HTTP based predecessor.
In our previous tutorials, we discussed how to use Couchbase Lite as a standalone embedded data store within your iOS app. In this post, we introduce a tutorial that will extend the capabilities of Couchbase Lite enabled clients to incorporate data sync using a Sync Gateway.
Data Synchronization
Data Synchronization is critical for enabling offline-first mobile apps. Offline-first apps are apps that regularly need network connectivity to function but are usable even under conditions of no or poor network connectivity. The two critical aspects of enabling this is
– The ability to locally store data on the device so it is always available to the user , whether online or offline
– The ability to sync data to the cloud so it is available to the users on other devices when online. This includes secure routing of data across devices, enforcement of access conflict and handling of data conflicts.
Tutorial
This tutorial will demonstrate how to –
- Setup the Couchbase Sync Gateway (in walrus mode) to sync content between multiple Couchbase Lite enabled clients. We will cover the basics of the Sync Gateway Configuration
- Configure your Sync Gateway to enforce data routing, access control and authorization. We will cover the basics of the Sync Function API
- Configure your Couchbase Lite clients for replication
- Use “Live Queries” or Query events within your Couchbase Lite clients to be asyncronously notified of changes
We will be usiing a Swift App as an example of a Couchbase Lite enabled client.
App Overview
We will be working with the “User Profile” app which we introduced in the Fundamentals Tutorial and extended in the Query Tutorial. In this tutorial, we will be extending that app to support data sync.
This version of the app does the following –
- Allows users to log in and create or update his/her user profile information. The user profile view is automatically updated everytime the profile information changes in the underlying database
- The user profile information is synced with a remote Sync Gateway which then syncs it to other devices (subject to access control and routing configurations specified in the
sync function
)
Next Steps
At the end of the tutorial, you should have an understanding of how to securely enable data synchronization using a Sync Gateway. You should gain familiarity with the fundamentals of how to enforce access control, data validation and authorization within your Sync Gateway as well as how to register for query events from within your app.
However, as you would observe from the tutorial, we are using Sync Gateway in “walrus mode” and hence, we are not persisting the data in the cloud.
Stay tuned for a future tutorial that will extend this system to incorporate the Couchbase server for data persistence in the cloud.
If you have questions or feedback, please leave a comment below or feel free to reach out to me at Twitter @rajagp or email me at priya.rajagopal@couchbase.com. The Couchbase Forums are another good place to reach out with questions.
Hi. I am doing some research for a project and was wondering if I were to use Couchbase Mobile offline, would it be as simple as just turning on the sync gateway capabilities to sync with Couchbase in the cloud? I would like to provide in-app purchase to allow this capability and the user may have a large dataset by the time they decide to use the cloud syncing capabilities.
You can use Couchbase Lite in standalone mode (as shown in the “standalone tutorial” linked in from this blog)- what that essentially implies is that you will not have the Couchbase Lite replicator setup to sync with Sync Gateway.
The Sync Gateway will be on and running regardless (so you are not turning on or off the Sync Gateway).
In your use case, you will control whether or not the client (mobile app with Couchbase Lite) will sync to the Sync Gateway based on whether the user makes the purchase. Once the user makes purchase, you can setup the replicator on Couchbase Lite to start syncing with Sync Gateway. Note that you will have to create the user on the Sync Gateway so it will authorize the sync. So when the in-app purchase is made, you can use the Sync Gateway /user REST endpoint to create the user for instance.
Great. This is what I was looking for! Thanks!