开发者

How to create a live feed using Rails

Im trying create a live feed block which identifies the latest data added to a forum discussion and automatically updates a block on another page with the latest posts on that forum. Im using Ruby on Rai开发者_如何学JAVAls and I would really appreciate any help on this.

(If my question is not clear I hope I can be more specific with one of these examples). I'm trying to build something like the rolling twitter updates that blogs sites have nowadays or wouldn't mind something like the twitter home page which automatically updates itself. I assume the twitter home page musst using some sort of polling feature.

Any help on how to go about building one of these would be awesome


These sites do this by regularly polling the server with XmlHttpRequests made by javascript. This is made possible by the setInterval function

The shortest version is something like this (jQuery, but the library translation would be simple):

setInterval(function() {
  $.getJSON('/posts.json', function(posts) {
    // code to remove old posts
    // code to insert new posts
  });
}, 5000);

The 5000 is the time in milliseconds between function-calling.

You would then have your posts controller just return the latest posts:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜