A few weeks ago I had written a guide that demonstrated saving captured images to Couchbase Lite as base64 encoded string data in a NativeScript with Angular mobile application. While the previous guide worked for both Android and iOS, the data was localized to the device. What if you wanted to synchronize the images between devices or even store them in the cloud?

We’re going to see how to use Couchbase Mobile to synchronize image data between devices and platforms in a NativeScript with Angular application.

Going forward you should note that this is part two in the series.  This means that if you haven’t already followed the previous tutorial and gotten a working version of the project, you should put this tutorial on hold. Start with the guide, Save Captured Images in a NativeScript Angular Application to Couchbase, then work your way into synchronizing the images.

NativeScript Couchbase Photos

The above animated image will give you a rough idea of what we’re after.  We want to be able to synchronize the saved images between Android and iOS using Sync Gateway and optionally Couchbase Server.

The Requirements

The prerequisites for this guide are similar to what was found in the previous.  You’ll need the following:

  • NativeScript CLI
  • Android SDK for Android or Xcode for iOS
  • Couchbase Sync Gateway
  • Couchbase Server (optional)

You’ll notice that Sync Gateway and optionally Couchbase Server are the new requirements of this guide in the series.  We’ll need these for synchronization to actually happen.  If you’re unfamiliar, Sync Gateway is the synchronization middleware and Couchbase Server is the remote database server.

Configuring Sync Gateway for Replication

To use Sync Gateway we’ll need to define a configuration as to how synchronization happens and things like that.

Create a sync-gateway-config.json file somewhere on your computer that contains the following information:

In the above configuration file we are saving everything to walrus:data which is an in-memory solution rather than persisting it to Couchbase Server.  The remote database is called image-database, but it doesn’t have to match what we have in our mobile application code.

For simplicity everyone will be able to read and write data in the same channel as a guest.

To run Sync Gateway, execute the following:

You should be able to access Sync Gateway from your web browser at http://localhost:4984/_admin/ and view everything that is being synchronized, often referred to as replicated.

Adding the Logic for Synchronizing Image Data

The actual code involved towards getting replication working in our NativeScript with Angular application is minimal.

Open the project’s app/app.component.ts file and include the following TypeScript code:

The above code includes everything from the first part of the series as well as this part of the series.  We’re going to break down only what has been added in regards to replication.

In the constructor method we define where we are going to push data to and where we are going to pull data from.

This is to be done continuously for as long as the application is open.

Make sure you use the correct host or IP address for Sync Gateway.  If you’re using Genymotion like I am, localhost will not work.  You’ll have to figure out the correct IP addresses.

In the ngOnInit method we start the replication process and configure a listener.

Any time there is a change in the database we will loop through them and load the base64 data.  This example is simple so there are no updates or deletions of images.  If this was the case, our listener would be a little more complex in logic.

The reason we are using an Angular NgZone is because the listener operates on a different thread.  By zoning, we can take the data and make sure the UI updates correctly.

That’s all we had to do to get the images synchronizing between device and server.  Easy right?


You just saw how to synchronize image data between devices and platforms using NativeScript, Angular, and Couchbase. This was a followup to the previous tutorial I wrote called, Save Captured Images in a NativeScript Angular Application to Couchbase, where we got the initial application up and running.

In case you’d rather not store your images in the database, you might consider creating an API that uses an object storage like Minio or Amazon S3.  I’ve written a tutorial on creating an API that saves to Minio that might help.

For more information on using Couchbase with Android and iOS, check out the Couchbase Developer Portal.


Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Leave a reply