Reputation: 3707
I am working on a webapp utilizing PouchDB as local database, and CouchDB as central database. One of the reasons i utilize PouchDB is i want to leverage offline support in my app. I've ran into a small issue however. When going offline and going online again, PouchDB doesn't sync anymore. I have to refresh the browsers to get it to start sync again. One solution would be that even though the application is offline PouchDB would keep polling the remote database even though offline, which would lead to that when it's online again the synchro would pick up again. Another solution would be to let the user manually tell the application it's online again and pick up the synchronization from there.
How can i tell PouchDB to start syncing again? If i can do this, i can solve my problem.
Upvotes: 4
Views: 1853
Reputation: 408
PouchDB 3.1.0 implements retry
option for replication API.
https://github.com/pouchdb/pouchdb/commit/47d105edaa9e36006124636235be8016c2e8c52c
PouchDB.replicate.sync('http://remote', {
live: true,
retry: true
})
Upvotes: 3
Reputation: 3341
I've just released pouchdb-persist, a plugin for persistent replication. With this plugin, you can just do
var db = new PouchDB('todos');
// Instead of db.replicate()
var persist = db.persist({ url: 'http://localhost:5984/todos' });
You can also listen for the connect
and disconnect
events.
Upvotes: 1
Reputation: 3341
My trick is to relaunch the replication whenever there is an error:
var retryMs = 2000;
function replicateFrom() {
var opts = {live: true};
db.replicate.from(remoteCouch, opts).on('error', function() {
console.log('replication error');
setTimeout(replicateFrom, retryMs);
});
}
The same can be done with db.replicate.to
Upvotes: 1
Reputation: 131
PouchDB's goal is to mirror CouchDB with feature parity, one feature of CouchDB's replication is that it will timeout after a while if it's offline so this'll require you to start replicating again as you've noted.
There is an open issue (https://github.com/pouchdb/pouchdb/issues/966) about infinite replication, so this would not be an issue but until then you can use the same replication call you used to start the replication in the first place:
db.replicate.to(remoteDB, [options]);
http://pouchdb.com/api.html#replication
One option would be to try something like http://github.hubspot.com/offline/docs/welcome/ but hopefully we can get this feature added to PouchDB.
Upvotes: 1