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 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.
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.
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
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 firstname.lastname@example.org. The Couchbase Forums are another good place to reach out with questions.