javascriptvue.jssynchronizationpouchdbcloudant

How to sync pouchdb with remote Coudant, not able to connect?


How do sync PoucDB to Cloudant by using user login? My Cloudant is set up with IAM and Legacy.

How do I register users and use the credentials to login to use Cloudant and PouchDB

I have not found answers in Cloudant, PouchDB docs. And do not see my answer in Stackoverflow

I am using the PouchDB docs:

var db = new PouchDB('todos');
const remoteUrl = "https://some address"
const remoteOptions = {
   'auth': { 'username': user , 'password': pass } }  
const remoteDb = new PouchDB(remoteUrl, remoteOptions);

localDB.sync(remoteDB).on('complete', function () {
  // yay, we're in sync!
}).on('error', function (err) {
  // boo, we hit an error!
});

Replication does work from Cloudant to on-premise Couchdb. Therefore I can connect to Coudant remotely.


Solution

  • As you have Legacy Auth available, I won't worry about IAM authentication. We can simply generate an API key/password for your mobile client and use legacy authentication.

    Creating an API key

    In the Cloudant Dashboard, choose the "Permissions" menu and click the "Generate API Key" button. You should see a generated Key and Password - these become the Username & Password in the URL you feed to PouchDB.

    If you're doing a sync (i.e data is to flow in both directions) then your API key needs both _reader & _writer permissions. If data is to flow from server to mobile only, then _reader & _replicator will suffice.

    Client-side code

    Your client side code is pretty much correct:

    e.g.

    const PouchDB = require('pouchdb')
    const db = new PouchDB('todos')
    const username = 'KEY'
    const password = 'PASSWORD'
    const host = 'mycloudservice.cloudant.com'
    const databaseName = 'todos'
    const remoteDB = `https://${username}:${password}@${host}/${databaseName}`
        
    db.sync(remoteDB).on('complete', async function () { 
     console.log('done')
    }).on('error', function (err) {
      console.error(err)
    });