1

I have a Rails app implementing a game, so it's expected that a player will leave a browser open at the game page. When player Alan takes an action himself, I use AJAX requests to update the game page Alan is viewing to reflect the new state. However, when another player (Bob) takes an action, I don't have (or want) a mechanism to push the change to Alan's view.

I would like Alan's page to periodically poll the Rails server to find if there have been any changes since last reload, and to reload the page (either via a whole-page GET or an AJAX call) if not. In order to play nicely with caches and proxies, I'd like to do this by issuing a periodic HTTP HEAD request, get Rails to work out the timestamp of the last change (trivially available from my DB) and respond with that in the Last-Modified header; then have the client-side act on that timestamp.

How can I go about doing this?

2 Answers 2

1

A lot of what you want is built-in to Rails.

Have a look at Conditional GETS in the Rails cache guide.

If you set this up right it all just happens between the browser and the server.

If you are very keen, you can put a reverse proxy in front of your Rails stack and have a whole extra layer of caching.

5
  • Neat. The link shows what needs to be done server-side; how do I get the client to send the appropriate headers?
    – Chowlett
    Apr 14, 2010 at 10:41
  • Hang on. What version of Rails is stale? first in? My server platform is running Rails 2.1.0 and can't be upgraded (I don't own it), and GamesController doesn't seem to respond to stale? or fresh_when
    – Chowlett
    Apr 14, 2010 at 10:55
  • Ah ... maybe it's a 2.3 feature. Perhaps there is an etag plugin for earlier versions?
    – Toby Hede
    Apr 14, 2010 at 13:02
  • Also: modern browsers handle conditional gets out-of-the-box.
    – Toby Hede
    Apr 14, 2010 at 13:03
  • In the end, I implemented my own solution whole-cloth; but I'm about to move to Heroku, so I may re-examine this.
    – Chowlett
    Apr 5, 2011 at 8:56
0

I don't have (or want) a mechanism to push the change to Alan's view.

Why don't you want realtime updates to the other players view?

You could achieve this with long polling AJAX requests like ajaxmessaging does (did?) or if Flash is your weapon of choice then Juggernaut could be an option. The nginx_http_push_module also looks really promising. It's fair to say that the whole deal is kind of a mess so let's hope for a surprisingly quick move to HTML5 and its WebSockets.

2
  • I am aware of Comet - but I don't own the hardware or uplink on which my server is running. Hence I want to keep CPU / memory occupancy down as well as bandwidth.
    – Chowlett
    Apr 14, 2010 at 10:49
  • Of those three I only see memory usage as a potential problem if you choose a Comet based solution. Bandwidth usage could probably even go down. Apr 14, 2010 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.