Database replication when running the application offline

I use PouchDB to store data offline and synchronize with a remote CouchDB database when the application goes online. It works well if the application starts on the network: PouchDB fires an event pausewhen the connection is interrupted and continues when the connection returns. Here is a sample code:

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

But when the application is launched offline, an error ( Failed to load resource: net::ERR_ADDRESS_UNREACHABLE)

access to the remote database is not possible, and PouchDB fires an event error( "Database encountered an unknown error", status 500). Even if the connection returns, replication will not work.

: , (, pause , )?

UPDATE: nlawson ! , :

function retryReplication() {
  var remoteDB = new PouchDB('http://129.199.80.62:5984/remotedb');
  localDB.replicate.to(remoteDB, {
  live: true,
  }).on('paused', function (info) {
    // replication was paused, usually because of a lost connection
  }).on('change', function (change) {
    // yo, something changed!
  }).on('active', function (info) {
    // replication was resumed
  }).on('error', function (err) {
    setTimeout(retryReplication, 5000);
    // totally unhandled error (shouldn't happen)
  });
};
+4
1

, Github, StackOverflow.:) .

retry , -. , !

retry . , .

: ​​ PouchDB 3.6.0.

+8

Source: https://habr.com/ru/post/1584763/


All Articles