Taxonomy Icon

Mobile Development

Secure mobile offline synchronization

Get the code Watch the demo

Summary

Learn how to develop secure offline-first mobile apps with encrypted and automated data synchronization. In this code pattern, learn how to implement secure offline synchronization in hybrid mobile apps by using JSONStore from IBM Mobile Foundation, the Ionic framework, IBM Cloudant, and IBM Cloud Object Store. You learn about implementing offline user authentication in mobile apps, storing data securely (with encryption) on the mobile device, and automatically synchronizing downstream and upstream data and images.

Description

To ensure that your enterprise mobile apps work seamlessly in both offline and online modes, you need to use encrypted on-device storage and automate data synchronization with the backend databases.

While you can achieve automatic data synchronization with CouchDB or Cloudant No SQL DB databases (because they implement the CouchDB Replication Protocol), JSONStore collections in IBM Mobile Foundation provides a powerful alternative for achieving offline sync in both hybrid and Android native or iOS native apps.

JSONStore provides persistent storage of JSON documents encrypted using AES 256-bit encryption, and automated data synchronization with CouchDB or Cloudant databases. In addition, the robust synchronization architecture of JSONStore allows you to extend synchronization to traditional systems of record such as RDBMS or ERP backends.

Synchronization architecture of JSONStore

When you have completed this code pattern, you will understand:

  • How to achieve offline user authentication in mobile apps using JSONStore.
  • How to store data securely on the device using encrypted JSONStore.
  • How to achieve downstream and upstream synchronization of data between a Cloudant database and the device using the automated data synchronization feature of JSONStore.
  • How to achieve downstream and upstream synchronization of images between Cloud Object Storage and the device using imgCache.js and Cordova File API.

Flow

Online scenario

Architecture diagram of secure online synchronization

  1. When there is network connectivity, user launches and logs in to the mobile app.
  2. The mobile app sends the user credentials to Mobile Foundation server for validation. Mobile Foundation server validates the user credentials and returns an appropriate response to the mobile app.
  3. If user authentication succeeds, mobile app initializes JSONStore collection with the current user credentials
  4. The mobile app initiates data synchronization with the Cloudant database by way of the Mobile foundation adapter.
  5. The Mobile Foundation sync adapter makes REST calls to the Cloudant database and returns synchronization data to the mobile app. The data that is fetched from the Cloudant database will have references to the images, which are stored on Cloud Object Storage.
  6. Mobile app makes a call to Mobile Foundation adapter, which makes a call to Cloud Object Storage, to get the authorization token. This token will be used by the Mobile app to access the Cloud Object Storage.
  7. The mobile app fetches the images using the image-caching plugin.
  8. On the mobile app, the synchronized data (from Cloudant) and images (from Cloud Object Storage) are downloaded and available for the user to interact with. User can view the detail page consisting of image and geo-location marked inside Google Maps.
  9. After the user views and updates the data in the mobile app, the mobile app stores the new data in the JSONStore collection, which automatically synchronizes the data to the Cloudant database and the images to the Cloud Object Storage by way of the Mobile Foundation adapter.

Other users who click on refresh button on the home page (and those who log in anew) are shown the updated list of problem reports.

Offline scenario

Architecture diagram of secure offline synchronization

The device must have been previously online, and user authentication must have previously succeeded, such that the JSONStore password was set.

  1. When the device is offline, the user launches and logs in to the mobile app.
  2. If the correct password is entered, the mobile app initializes the JSONStore collection with the credentials.
  3. If user authentication succeeds (that is, JSONStore initializes successfully), the mobile app reads data from the previously synchronized JSONStore collection and shows the list of items on the Home page.
  4. The user can view the detail pages for the items in the list. If the detail page was previously viewed when the device was online, the image has been cached and is viewable offline. The Cordova plugin for Google Maps ensures that the map view works in offline mode.
  5. The user can report new civic problems, and capture images and geo location information.
  6. The mobile app stores the new data in the JSONStore collection and the image and its thumbnail on local file storage on the mobile device.
  7. At at later time, when the device comes online, the mobile app automatically initiates the synchronization of JSONStore collection with the Cloudant database by making a call to MFP sync adapter, which posts the new data to the Cloudant database.
  8. The mobile app fetches the authorization token for interacting with the Cloud Object Storage service by making a call to MFP adapter, and then uploads the new images to Cloud Object Storage.

Other users who click on refresh button on the home page (and those who log in anew) can see the newly reported civic problem and its details.

Instructions

Find the detailed steps for this pattern in the README. Those steps will show you how to:

  1. Setup Ionic and MFP CLI.
  2. Create the Cloudant database and populate it with sample data.
  3. Create IBM Cloud Object Storage service and populate it with sample data.
  4. Create Mobile Foundation service and configure MFP CLI.
  5. Download the source repo and customize it.
  6. Deploy the MFP adapters and test them.
  7. Run the application on an Android phone.
  8. Test the app functionality in offline mode.