long polling with jquery+jsonp+couchdb (cross domain supported)
Translation of the original in Catalan. Read it in: CatalàCastellano
I have spent months dreaming about doing the proof of concept I explain in this article, I will try to describe what it consists of but I warn you right away that the thing is a bit tricky.
Required features:
-
long polling: the goal is to receive the changes of a couchdb database in real time without having to keep asking whether there are changes, but rather they are sent every time they happen, automatically.
-
Through jQuery what I want is to update a web page asynchronously, so that the new results that come into the DB are shown on the screen in real time.
-
JSONP, it is a technique that allows us to receive the JSON output of CouchDB and then call a JavaScript callback function. The problem is that jQuery's original jQuery.getJSON() function has some shortcomings that, thanks to the jQuery-JSONP plugin, we can fix, these are: (copy/paste from the plugin website)
-
error recovery in case of network failure or ill-formed JSON responses,
-
precise control over callback naming and how it is transmitted in the URL,
-
multiple requests with the same callback name running concurrently,
-
two caching mechanisms (browser-based and page based),
-
the possibility to manually abort the request just like any other AJAX request,
-
a timeout mechanism.
-
CouchDB is a document based NoSQL database that is able to emit a signal (trigger) every time the content of a database changes. For more information about the topic you can check: CouchDB: The Definitive Guide in the chapter Change Notifications.
-
Cross-domain: when an XmlHttpRequest request (the basis of AJAX) is launched with JavaScript we have the limitation of only being able to do it on the domain that serves the web page, no other port or subdomain. Obviously not another host either. To skip this restriction is when we need to resort to JSONP.
The proof of concept has been to create a database called: notifcations where documents are stored that are notifications to be shown on the web page.
Then I have programmed the following web page:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery.jsonp-1.1.4.js"></script>
<script type="text/javascript">
function longpoll(since) {
var url = "http://IP_COUCHDB_SERVER:5984/notifications/_changes?include_docs=true&feed=longpoll&since="+since+"&callback=?";
console.log("since="+since);
$.jsonp({
"url":url,
"success":function(data) {
//console.log(data);
since=data.last_seq;
try {
console.log(data.results[0].doc.msg);
} catch(err) {
console.log("error:"+err);
};
longpoll(since);
},
"error":function(msg) {
//console.log(msg);
console.log('capturat error');
}
});
};
var url = "http://IP_COUCHDB_SERVER:5984/notifications?callback=?";
$.jsonp({
"url":url,
"success":function(data) {
//console.log(data);
longpoll(data.update_seq);
},
"error":function(msg) {
console.log(msg);
}
});
</script>
</head>
<body>
cos
</body>
</html>
The code is quite simple to follow, first of all the libraries are loaded: jQuery 1.4.2 and jquery-jsonp 1.1.4, both necessary to call the $.jsonp method, which is what will really do the job.
Next the longpoll function is declared, which takes as a parameter the number that indicates which has been the last change in the database. This is used to build the request made to CouchDB:
var url = "http://IP_COUCHDB_SERVER:5984/notifications/_changes?include_docs=true&feed=longpoll&since="+since+"&callback=?";
What the URL does is request the following:
-
the changes (_changes)
-
including the documents that have changed (include_docs=true)
-
treat the request as long polling (feed=longpoll)
-
show the changes since version X (since=X)
-
when you send the changes make a call to the callback function defined here (callback=?)
-
'?' is replaced by jquery-jsonp with the function named 'C', this name can be changed using parameters in the declaration of $.jsonp() that comes next
The 'console.log()' style messages are there to keep track of what is happening in the browser's javascript console.
$.jsonp() has quite a few possible parameters defined in the API, but in this proof of concept I only use 'url', 'success' and 'error'. The first one is clear what it is, the other two are the functions to call when the action goes well or badly respectively. Inside the function we put the code referring to the actions we want to do, for example, update the current page. Since this is only a proof of concept what I do is show messages on the console and that's it. The most important part is to notice that when the thing has gone well a call is made to itself so that it never ends. In fact when there is an error one could also make a call to itself so that you never stop trying to launch the request over and over again, but what I have done to test is that it is notified on the javascript console and that's it.
Outside the longpoll function what is done is a JSONP request to find out which is the last sequence number of the database, a parameter needed to enter for the first time into the recursive longpoll function.
Conclusions
It may all seem a bit convoluted but I would say that I have simplified the problem a lot, since until now I had been theorising a great deal about the topic. Until yesterday and the day before I had to get down to trying it in practice to know exactly how it could be implemented. From my point of view everything has turned out quite clean and understandable.
For those of you who are web programmers you will usually have had similar needs, so I hope it can be as useful to you as it was to me; in fact, for a few weeks now I have been working with Tiny Core Linux setting up a Quiet PC on a 512MB DOM to use it as a monitoring system for my networks and those of some clients, I hope that in a few months I can give you more information about the project.
2 comments
Comments from the old blog, as they were written. Comments are closed.
Oriol Rius
He trobat una applicació per CouchDB que intenta solucionar el problema del cross-domain al fer peticions amb JavaScript, malgrat això no implementa el tema del long-polling.
La solució es basa en la mateixa idea i és força elegant, tot s'ha de dir.
http://github.com/benvinegar/couchdb-xd
Oriol Rius
La nova versió de google search també usa JSONP i en aqest document que enllaço ens explica com evitar que les peticions fetes amb JSONP provoquin que una pàgina quedi infinitament en càrrega.
http://googlecode.blogspot.com/2010/11/instant-previews-under-hood.html?spref=tw