Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB real-time Ajax push update

I'm working on a simple PHP application, using CouchDB and PHP-on-Couch to access some views, and it's working great. My next step is to introduce Ajax to update the frontend with data from the database.

I understand you can use the _changes notifications to detect any changes made on the database easily enough. So, its a matter of index.html monitoring for changes (through long polling), which calls loadView.php to update the page content.

Firstly, I hope the above is the correct method of going about it...

Secondly, when browsing to index.html, the page seems to never fully load (page load bar never completes). When a change is made, Firebug shows a the results as expected, but not any subsequent changes. At this time, the page seems to have stopped the infinite loading.

So far, i'm using jQuery to make the Ajax call...

$.getJSON('http://localhost:5984/db?callback=?', function(db) {
    console.log(db.update_seq);
    $.getJSON('http://localhost:5984/db/_changes?since='+db.update_seq+'&feed=continuous&callback=?', function(changes) {
        console.log(changes);
    });
});

Any ideas what could be happening here?

like image 502
crawf Avatar asked Jun 30 '11 06:06

crawf


1 Answers

I believe the answer is simple enough.

A longpoll query is AJAX, guaranteed to respond only once, like fetching HTML or an image. It may take a little while to respond while it waits for a change; or it may reply immediately if changes have already happened.

A continuous query is COMET. It will never "finish" the HTTP reply, it will keep the connection open forever (except for errors, crashes, etc). Every time a change happens, zoom, Couch sends it to you.

So in other words, try changing feed=longpoll to feed=continuous and see if that solves it.

For background, I suggest the CouchDB Definitive Guide on change notifications and of course the excellent Couchbase Single Server changes API documentation.

like image 186
JasonSmith Avatar answered Sep 25 '22 14:09

JasonSmith