How to find out when we lost synchronization with remote CouchDB

I have a mobile application using PouchDB that listens for changes from a remote CouchDB server. Sometimes an application pushes its own changes. Is there a way to check if I have an active “connection” to a remote instance of CouchDB?

The best solution I can come up with so far is to call db.info periodically and see if I get an error, but that seems a bit risky.

+4
source share
4 answers

, pouchdb, PouchDB PouchDB CouchDB. , PouchDB , sync replicate , docs, error.

0

paused, , ?

localDB.replicate.to(remoteDB, {
  live: true,
  retry: true,
}).on('paused', (err) =>{
  console.log('paused');
  if (err) {
    alert(`No connection! ${err}`);
  }
  // replication was paused, usually because of a lost connection
}).on('change', (change)=>{
  // yo, something changed!
}).on('active', (info)=>{
  // replication was resumed
}).on('error', (err)=>{
  // totally unhandled error (shouldn't happen)
});

, paused , , err - undefined. , , paused, err.

. , , err.

PouchDB:

paused (err) - , , , .

0

" " false .

API:

error (err) - , ​​- . , , ( ).

:

    // Sync pouch - couch
db.sync(couch, {
    live: true,
    retry: false
}).on('error', (err) => {
    console.log(`A sync error occurred: ${err}`);
});
0

, PouchDB , , - -. , .

, PouchDB, :

, , :

, .

, active change, pouchDbSyncActiveEvent pouchDbSyncChangeEvent , false. pouchDbSyncActiveEvent true pouchDbSyncActiveEvent 'active' . , . pouchDbSyncChangeEvent true pouchDbSyncChangeEvent 'change'. , pouchDbSyncChangeEvent - false, 'paused' . , .

:

let pouchDbSyncActiveEvent = false
let pouchDbSyncChangeEvent = false

localPouchDb.sync(remoteCouchDb, {
  live: true,
  retry: true
})
.on('active', (change) => {

  pouchDbSyncActiveEvent = true

})
.on('change', (change) => {

  pouchDbSyncChangeEvent = true

})
.on('paused', (info) => {

  if(pouchDbSyncActiveEvent == true && pouchDbSyncChangeEvent == false){

    // Gotcha! Syncing with remote DB not happening!

  }
  else {

    // Everything ok. Syncing with remote DB happening normally.

  }

  pouchDbSyncActiveEvent = false
  pouchDbSyncChangeEvent = false

})

. .on .on .on , , :)

0
source

All Articles