{"id":8091,"date":"2015-11-26T13:36:18","date_gmt":"2015-11-26T13:36:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/26\/pouchdb-database-replication-when-the-application-is-launched-offline-open-source-projects-pouchdb-pouchdb\/"},"modified":"2022-08-30T15:03:01","modified_gmt":"2022-08-30T15:03:01","slug":"pouchdb-database-replication-when-the-application-is-launched-offline-open-source-projects-pouchdb-pouchdb","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/26\/pouchdb-database-replication-when-the-application-is-launched-offline-open-source-projects-pouchdb-pouchdb\/","title":{"rendered":"PouchDB: database replication when the application is launched offline-open source projects pouchdb\/pouchdb"},"content":{"rendered":"<p>I use PouchDB to store data offline and synchronize with a remote CouchDB database when the application goes online. It works well if the app is launched online: PouchDB triggers the <code>pause<\/code> event when the connexion is interrupted and continues when the connexion comes back. Here is a sample code:<\/p>\n<pre><code>localDB.replicate.to(remoteDB, {\n  live: true,\n  retry: true\n}).on('paused', function (info) {\n  \/\/ replication was paused, usually because of a lost connection\n}).on('change', function (change) {\n  \/\/ yo, something changed!\n}).on('active', function (info) {\n  \/\/ replication was resumed\n}).on('error', function (err) {\n  console.log(err);\n  \/\/ totally unhandled error (shouldn't happen)\n})\n<\/code><\/pre>\n<p>But when the application is launched offline, an error is encountered (<code>Failed to load resource: net::ERR_ADDRESS_UNREACHABLE<\/code>)<\/p>\n<p>the remote database can&#8217;t be accessed and PouchDB triggers an <code>error<\/code> event (<code>\"Database encountered an unknown error\"<\/code>, status 500). Even if the connexion comes back, the replication will not work.<\/p>\n<p><strong>Question:<\/strong> Is there a way to make the replication work even if the application is launched offline (for example by making the <code>pause<\/code> event comes first even if there is no access to the remote database yet)?<\/p>\n<p><em>UPDATE:<\/em> Thanks to nlawson for the answer! I just had to add the remote database creation in the function to make it work:<\/p>\n<pre><code>function retryReplication() {\n  var remoteDB = new PouchDB('http:\/\/129.199.80.62:5984\/remotedb');\n  localDB.replicate.to(remoteDB, {\n  live: true,\n  }).on('paused', function (info) {\n    \/\/ replication was paused, usually because of a lost connection\n  }).on('change', function (change) {\n    \/\/ yo, something changed!\n  }).on('active', function (info) {\n    \/\/ replication was resumed\n  }).on('error', function (err) {\n    setTimeout(retryReplication, 5000);\n    \/\/ totally unhandled error (shouldn't happen)\n  });\n};\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I use PouchDB to store data offline and synchronize with a remote CouchDB database when the application goes online. It works well if the app is launched online: PouchDB triggers the pause event when the connexion is interrupted and continues when the connexion comes back. Here is a sample code: localDB.replicate.to(remoteDB, { live: true, retry: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8091","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8091"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8091\/revisions"}],"predecessor-version":[{"id":8632,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8091\/revisions\/8632"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}