Rails: periodically use HEAD to check for page changes

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-14T10:11:40Z Indexed on 2010/04/14 10:13 UTC
Read the original article Hit count: 453

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails