Skip to content

Instantly share code, notes, and snippets.

@ismasan
Created November 8, 2009 16:16
Show Gist options
  • Save ismasan/229343 to your computer and use it in GitHub Desktop.
Save ismasan/229343 to your computer and use it in GitHub Desktop.
/* Jquery plugin for quick Comet long polling connections
Nothing special about this (just a recursive Ajax call).
The key is having the server hold connections open until there's data to push back to the client.
This can be done with Nginx and the nginx_http_push_module (http://github.com/slact/nginx_http_push_module)
---------------------------------------------------------------------------*/
(function($){
$.comet = function(url, success_callback, error_callback){
error_callback = error_callback || function(a,b,c){alert('Error '+b)};
$.ajax({
type: "GET",
dataType: 'json',
url: url,
success: function(data){
success_callback(data);
$.comet(url, success_callback, error_callback);
},
error: function(a,b,c){
error_callback(a,b,c);
$.comet(url, success_callback, error_callback);
},
ifModified: true
});
}
})(jQuery);
/*
Usage
------------------------------------------------------*/
$.comet(function(data){
$('#content').prepend(data.someAttribute)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment