These sites do this by regularly polling the server using the XmlHttpRequests created by javascript. This is made possible by the function.setInterval
The shortest version is something like this (jQuery, but translating the library would be simple):
setInterval(function() {
$.getJSON('/posts.json', function(posts) {
});
}, 5000);
5000 is the time, in milliseconds, between a function call.
You will then have your message controller, just return the latest messages:
class PostsController < ApplicationController
@posts = Post.find(:all, :order => 'created_at desc')
respond_to do |format|
format.html
format.json { render :json => @posts.to_json }
end
end
source
share