Pouchdb: Replicate {live: true} stops after a network problem

Created on 12 Nov 2014  Â·  6Comments  Â·  Source: pouchdb/pouchdb

Ran into this while developing a realtime Ionic web app for mobiles.
The app is syncing the clients to CouchDB using {live: true}, and the tests showed it is possible to create the same realtime functionality as Firebase has.

But if a client drops network connection during the sync, the live sync will stop completely with:
r {message: undefined, status: 405, statusText: "Method Not Allowed", result: Object, name: "unknown_error"…}

Using mobile networks, this is often the case, and optimally, the sync should recover automatically?
Maybe retry after a timeout?

Most helpful comment

Thanks Calvin - seems to work very well, too.

So for everyone with the same intent: forget about the snippet above - just use:

  PouchDB.sync('localDbName', 'http://couchdb.host.address:5984/remoteDbName', {live: true, retry: true});

All 6 comments

So right now it is up to the user to retry the sync manually when the 'complete' even is fired, this mirrors how replication worked with pouchdb, however we adding the ability to have replication automatically restart, its being tracked in https://github.com/pouchdb/pouchdb/issues/2768, the code is in there it just isnt turned on by default yet, hopefully by the next release

Thanks!
For now, I ended up using the snippet Nolan provided + Angular $timeout.

Works perfectly, reconnects automatically when network is available again.

            .on('error', function(err) 
                // 409 = conflict, no need to restart
                if (err.status != 409) {
                    $timeout(function(){
                        retryTimeout = Math.round(retryTimeout * BACKOFF);
                        _startSync();
                    }, retryTimeout);
                }
            });

Thanks for the snippet, might help someone else! :+1:

You can set retry true when you start replication too
On Nov 12, 2014 5:40 AM, "Nick Colley" [email protected] wrote:

Thanks for the snippet, might help someone else!

—
Reply to this email directly or view it on GitHub
https://github.com/pouchdb/pouchdb/issues/2979#issuecomment-62699650.

Thanks Calvin - seems to work very well, too.

So for everyone with the same intent: forget about the snippet above - just use:

  PouchDB.sync('localDbName', 'http://couchdb.host.address:5984/remoteDbName', {live: true, retry: true});

I face a simular problem even when using this:

PouchDB.sync('localDbName', 'http://couchdb.host.address:5984/remoteDbName', {live: true, retry: true});

The Replication object for "pull" direction is in state "stopped" after the remote CouchDB was not reachable due to network problems (no Wifi connection).

Does this retry option stop at some condition?

Was this page helpful?
0 / 5 - 0 ratings