#229 Polling for Changes
Aug 30, 2010 | 16 minutes |
Ajax
If you have frequently changing data on the server side, it's helpful to automatically display this to the user as well. Here I show how to accomplish this with polling in jQuery.
- Download:
- source codeProject Files in Zip (107 KB)
- mp4Full Size H.264 Video (24.3 MB)
- m4vSmaller H.264 Video (16.5 MB)
- webmFull Size VP8 Video (41.4 MB)
- ogvFull Size Theora Video (34.3 MB)
it's really nice to have jquery in rails3.
thanks for this Ryan!
great one as always..
is there a simple way to make it a pull and not a push, so the server will not get lots of hits.. (you said something about a socket.. is this is the only way?)
it will be nice to have something as Gmail notify only when a new msg arrived.
Thanks
Elad, I'm guessing you actually meant that you want server push and with Rails combined with Nginx there is one way forward with a HTTP push module for Nginx: http://github.com/slact/nginx_http_push_module
Here's an article describing how it could work: http://www.igvita.com/2009/10/21/nginx-comet-low-latency-server-push/
Cheers
Elad, you can use WebSockets: http://en.wikipedia.org/wiki/WebSockets
I've had a lot of success switching my polling-based notification system to node.js & faye. Database traffic dropped by over 70% for my app, since node could push the data and completely avoid the db. Crazy!
Thanks so much for this! I wish I had seen this before.. I'm going to go redo my comments area now! By the way, did you experience any issues upgrading to Rails3?
Practical and straightforward as usual. Thanks, Ryan.
However, I have never been comfortable with having the server return a big chunk of escaped JavaScript that inserts HTML into a particular page. The problem is that the server has to know the structure of the target page (dom IDs, etc) so the response can only work with that particular page. I prefer the server to return a plain HTML response and the page to act as a client that receives and inserts that HTML properly. That way if we have two differently-structured pages that need to poll for new comments they can use the same URL (controller/action). This makes the server code simpler and more generalized and the JavaScript less fragmented because the page's full behavior is all coded in one place (you don't need index.js.erb).
This is easy to implement by listening for AJAX events, as detailed here:
http://code.alexreisner.com/articles/link-to-remote-with-ujs.html
Just a thought. Thanks again for the great screencast.
Great episode! But I noticed that you are using clas selector in jquery, which is very slow in IE. I think it's better to write at least like this one:
$("input.comment")
Just put the element before the class name and it will improve the performance.
Best to display it to the user as a progress bar in my opinion. That way you keep him distracted instead of just displaying a 'please wait' message :)
Hi!
Great screencast as usual. Nothing new to me (as I earn my beard by writing jQuery), but I sent it to a friend, which was curios how to do Ajax.
Do you plan to add a Flattr button to your page? Flattr a smart mix between the like button and donating, but not as clumsy as Paypal and it's getting very popular in Europe right now. I would click every single episode!
Regards from Germany,
Johannes
I'm pretty new to rails but does using "raw" open you up to any sort of security concerns? For example if you added a comment that had javascript in it like so: <script language="javascript">alert('hello')</script> would that run because you're using raw?
@Dan: no, because of the escape_javascript function.
Thanks Javi. Good to know.
@Greg @Igor Thanks!
I know "push" is a bit of a buzz word at the moment but it's completely justified and you really should give it a try.
Using Pusher - http://pusherapp.com - you get WebSockets (and a Flash fallback) that can be integrated with your Rails app with just a few lines. Check out the quickstart guide http://pusherapp.com/docs/quickstart to see how easy it is. You also get instant feedback - no waiting for 10 seconds - and less load on your server to boot!
Disclaimer: I work at New Bamboo who are the guys behind Pusher.
Hey Ryan. Just a tip, instead of doing "<%= escape_javascript(render(@comments)) %>" you can do just (without quotes) <%= (render(@comments)).to_s.to_json %>
i find it a nice trick
Hi Ryan!
Thanks for the great things you're doing! :)
I have a question about this episode.
In the controller code in where condition you compare created_at with Time.at(params[:after].to_i + 1). In rails 2.3.8 datetimes are stored in utc in DB, and Time.at(...) would return a time in the system timezone so inserting Time.at into where condition without explicitly converting it to utc would produce incorrect resulting query. Did they change the way conditions work in rails3?
Thanks Ryan for this. I was just working om something like this for one of my projects.
Have you seen Robert Fischer jQuery port of PeriodicalUpdater ( http://github.com/RobertFischer/JQuery-PeriodicalUpdater ) That can do what you build yourself here with just a small change :-)
Please look at it, I found it very usefull.
What about an RJS template instead? It would cut down on the js awkwardness and would be compatible with respond_with.
@Mike That would mean going to prototype, unless you use something like jrails, not sure if its compatible with Rails 3.
Chainging isn t so Ok. Maybe do something else. It is more secure.
This is nice but isn't the more modern way of doing this using a web socket or something like comet?
Any chance of adding a HTTP streaming webcast :) with something like orbited or APE?
Ryan (or anyone): One question: The client is effectively doing a GET, right? And GETs are supposed to be idempotent (not change the state on the server). Do you have to do anything special to tell the server's cacheing mechanisms that a GET on the same URI twice in a row might yield different results? Or is it just luck that the &after= query parameter has that effect (since it changes continually)?
Hi everyone, can somebody take a look in this question:
http://stackoverflow.com/questions/7882084/while-polling-server-how-to-update-the-content-of-a-lightbox-using-rails-3
I'm trying to update a lightbox, instead of the normal view.
thanks
awesome
where can I find a revised and updated demo/tut for something like Rails3.1.2 "polling-for-changes" like this great but not updated screencasts: http://railscasts.com/episodes/229-polling-for-changes?view=asciicast ?
answer also here if u want
http://stackoverflow.com/questions/8760791/where-can-i-find-a-revised-and-updated-demo-tut-for-something-like-rails3-1-2-p
thank you
Whoever watches this and prefers pushing over polling, check out https://github.com/dchacke/entangled
Entangled is a Rails gem that pushes data changes to all clients in real time. Maybe it helps someone!