views:

601

answers:

2

Hi,

I'm looking for a good way to implement real time feed for my users on a Rails app (Phusion passenger server). Each feed can be different depending on the user and I expect to have one new item every 20 - 60s. Periodic ajax request doesn't look like the best way to do it for me.

I heard about Comet and I thought about having something like this: - Using a XMLHttpRequest long polling to wait for a ping from the server - Once the server sends a ping request the latest items with ajax - Start another XMLHttpRequest

Is there something wrong with this? Are there easier and better ways to do that?

Thanks, S.

+2  A: 

In terms of web applications (and by extension Rails applications), real time is just an illusion. Long polling is a very close approximation. Unfortunately it's not well suited to Rails. Even less so on Passenger.

Long polling requires a persistent open connection for each user, which doesn't scale very on servers that weren't designed to handle it (such as Apache). Unfortunately there really many servers designed for long polling scalability that play well with Rails. You could try the Shooting-Star server, but I really have no idea how its performance compares to Passenger, for your standard requests.

My personal opinion of long polling is it's a solution in need of a problem.

Really you should be asking yourself the following questions:

  • Are these updates of a high enough priority that they cannot wait 40 seconds?
  • What will happen if updates are not received immediately?
  • Are my users going to so focused on my application that waiting 15 seconds is going to impact their experience negatively?
  • What percentage of a user's attention does my application attract under normal use?
  • How long does it take to respond to an update?
  • Does it really need to be real time?

A few of those questions are asking other questions in a different way, but that's kind of necessary with such subjective questions.

I think you see what I'm getting at: Real time updates are very nice to have, but never really necessary. If you are working on something where the consequence of failing to react to a real time update is the end of the world. You really should not be developing it as a web application at all.

If you've still got your mind on Real Time updates you can look into Juggernaut. But that's a Flash based solution.

EmFi
+3  A: 

Friendfeed built the Tornado server in Python which they've open sourced.

We looked at a number of XMPP options that are quite complicated to set up, before going with the nginx_http_push_module. The long-lived HTTP GET requests connect to this, and the rails app pushes requests back at nginx. Nginx also proxies dynamic requests to a cluster of mongrels. We have some jQuery running that opens the connection then re-opens it to the server when it receives a message, or when there is an error. In this way we're able to achieve near-realtime updates in the Comet style.

This blog post on the nginx module with a ruby example should help you get started (have to compile nginx). We're running this in development now and plan to use this in production unless it proves itself unreliable, so far so good.

Andy Atkinson