Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Knockout.js with CouchDB - updating when changed

Just wondering about the best way to subscribe to my CouchDB data store, so that if a document in couch is updated, the KO view will also update (automagically). Is this something that's even possible?

Below is what I have so far, which simply get the user name from the user_info document.

$.getJSON('http://localhost/couchdb/user_info', function(data) {
    var viewModel = ko.mapping.fromJS(data);
    ko.applyBindings(viewModel);    
});

Any help would be greatly appreciated!

like image 403
crawf Avatar asked Aug 09 '11 05:08

crawf


2 Answers

CouchDB supports notifications when documents change: the changes feed.

You can poll the changes feed, with a ?since=X parameter to receive only updates since X.

You can also "long poll" the feed by adding &feed=longpoll. If there are no changes yet, CouchDB will receive your query but not answer until finally a change comes on.

Or, you can have a full COMET-style feed by instead adding &feed=continuous. That is similar to longpoll, however CouchDB will never close the connection. Every time a change happens, it will send you the JSON and then continue waiting.

Finally, you can be notified when anything changes in the database, or you can specify a Javascript filter to run on the server (&filter=designdoc/filtername). You will only receive notifications if the filter approves.

like image 195
JasonSmith Avatar answered Oct 19 '22 08:10

JasonSmith


Have you looked at http://hood.ie/ it woks well. I'm also running hoodie as an os_daemons service from within my couchdb.

It's nice.

like image 29
thanos Avatar answered Oct 19 '22 08:10

thanos