Using PouchDB for Offline/Data Sync
Recently the term âMobile Firstâ received additional notoriety as the new CEO of Microsoft proclaimed the companyâs shift in focus. As Iâve been researching mobile frameworks lately, Iâve run across another term â âOffline First.â
As much as you may be online with your mobile phone or tablet, inevitably there will be times when youâre not connected, but still need to work. Itâs in these times that mobile apps with offline/data sync capabilities shine. Not all app publishers want to store data on public clouds like iCloud, especially with recent security breaches. Several other options exist, and for this post, Iâm going to look at the mobile side of one contender.
CouchDB and Data Synchronization
CouchDB has been around for quite some time, and although it hasnât received the same attention as MongoDB, it has a very compelling feature. Its data replication/synchronization feature is quite robust and mature. It offers ânear-timeâ replication with the primary focus of fail-over for clusters of CouchDB servers.
In fact, âCouchâ is an acronym for Cluster of unreliable commodity hardware, and the CouchDB team chose the approach of âeventual data consistencyâ over immediate mirroring. So, if youâre looking for real-time updates on your mobile app, you should probably look into other alternatives like Socket.IO, etc. However, Iâve found that ânear-timeâ is close enough (virtually indistinguishable) for my purposes.
Several client libraries have sprung up around the CouchDB replication protocol (over HTTP) to make it easier for developers to store data locally and sync to remote servers when an internet connection is available. Iâve been looking into a few with one in particular that should be of interest to hybrid app developers.
PouchDB â a JavaScript CouchDB Implementation
PouchDB offers not only client data replication to remote servers, but also provides a robust key/value data store for the browser as well. And not only the browser, but any JavaScript environment with disk access, including Node.js.
The PouchDB web site has an introductory tutorial that steps you through the process of building a simple single page app (SPA). The app is modeled after the vanilla JavaScript implementation of the excellent TodoMVC benchmark for JavaScript frameworks. If youâre looking for a great comparison of SPA frameworks by the way, Iâd highly recommend checking out TodoMVC.
In the PouchDB tutorial, they demonstrate the data sync capabilities using a hosted CouchDB instance running on Iris Couch. I have committed the completed tutorial code on GitHub using a local Node/Express/PouchDB server to demo its data sync capabilities. This way you can play around with the code without having to sign up for a hosted CouchDB instance on Iris.
Note that if youâre planning to run this on Windows, youâll need to be able to run node-gyp to build one of the Node modules. Iâve been able to set up my environment on Windows, but would recommend just running a Linux VM instead.
Clone, Run and Explore
As simple as:
git clone https://github.com/DJacksonKHS/todo-sync.git cd todo-sync npm install npm start
Then, browse to http://localhost:3000 to check out the app. Open a second tab in your browser to see your changes sync across clients.
Iâm also putting together a simple Cordova/PhoneGap app that I will commit to another repo, so check back soon to see this app running on a mobile device. It will include a running CouchDB or PouchDB Server instance, probably on Iris Couch or Heroku.
Conclusion
PouchDB has adapters for at least a few mobile SPA frameworks, including Backbone and Ember. I was unable to get the Ember adapter working properly â still not ready for prime time. I havenât finished integrating the Backbone adapter yet, but will probably use it for the packaged hybrid app if I canât get Ember working soon.
If youâre looking into offline data sync for your mobile hybrid app, I highly recommend you look into PouchDB. Not only does it have an excellent data sync feature, but the key/value store modeled after CouchDB is also a great way to store and cache data locally. Hopefully this simple introduction will be helpful as you evaluate your options.
| Reference: | Using PouchDB for Offline/Data Sync from our JCG partner Dave Jackson at the Keyhole Software blog. |


