CouchDB _ changes notifications - using jquery.couch.js couch.app.db.changes ()

I have replication running in CouchDB and I want to update my interface when the changes are transferred to the target database. I read about the _changes database API and found the couch.app.db.changes () function in jquery.couch.js however I cannot decide how to use this function. I assume I need to set up a listener, but my knowledge of Javascript is not yet what it should be.

Unfortunately, the documents http://www.couch.io/page/library-jquery-couch-js-database do not even list the changes () function.

Can someone help me, and also let me know what the parameters are for.

Here is the code for the function in question:

changes: function(since, options) { options = options || {}; // set up the promise object within a closure for this handler var timeout = 100, db = this, active = true, listeners = [], promise = { onChange : function(fun) { listeners.push(fun); }, stop : function() { active = false; } }; // call each listener when there is a change function triggerListeners(resp) { $.each(listeners, function() { this(resp); }); }; // when there is a change, call any listeners, then check for another change options.success = function(resp) { timeout = 100; if (active) { since = resp.last_seq; triggerListeners(resp); getChangesSince(); }; }; options.error = function() { if (active) { setTimeout(getChangesSince, timeout); timeout = timeout * 2; } }; // actually make the changes request function getChangesSince() { var opts = $.extend({heartbeat : 10 * 1000}, options, { feed : "longpoll", since : since }); ajax( {url: db.uri + "_changes"+encodeOptions(opts)}, options, "Error connecting to "+db.uri+"/_changes." ); } // start the first request if (since) { getChangesSince(); } else { db.info({ success : function(info) { since = info.update_seq; getChangesSince(); } }); } return promise; }, 
+4
source share
4 answers

Alternatively, you can use the longpoll change feed. Here is one example:

  function bind_db_changes(database, callback) { $.getJSON("/" + database, function(db) { $.getJSON("/"+ database + "/_changes?since="+ db.update_seq +"&heartbeat=10000&feed=longpoll", function(changes) { if($.isFunction(callback)){ callback.call(this, changes); bind_db_changes(database, callback); } }); }); }; bind_db_changes("test", function(changes){ $('ul').append("<li>"+ changes.last_seq +"</li>"); }); 
+3
source

Note that $ .couch.db.changes is now in the official documentation:

http://daleharvey.github.com/jquery.couch.js-docs/symbols/%24.couch.db.changes.html

Also a good example of using _changes with the jquery.couch plugin:

http://bradley-holt.com/2011/07/couchdb-jquery-plugin-reference

+2
source

how about using ajax-feateures jquery?

 function get_changes() { $.getJSON("/path/to/_changes", function(changes) { $.each(changes, function() { $("<li>").html(this.text).prependTo(mychanges_div); }); get_changes(); }); } setTimeout(get_changes, 1000); 
0
source

I am working with JS Promises code which allowed mt to understand the CounchDB code I posted above. Here is an example:

 var promise_changes = app.db.changes(); // Add our deferred callback function. We can add as many of these as we want. promise_changes.onChange( db_changes ); // called whenever this db changes. function db_changes( resp ) { console.log( "db_changes: ", resp ); } 

Google Chrome goes Busy with a long poll, which I hope they will resolve one fine day.

0
source

All Articles