Here's the script from a talk I gave yesterday at Ruby on Rails Oceania Sydney. Hi all, I'm Chris Hulbert. During the day i work as a C# programmer, but don't hold that against me. I'm new here so be nice. I'm here to talk about Comet. Comet is a handy technique for allowing the server to notify the client about any event. Yes, welcome to windows 311 programming again, we're talking event driven programming, but for websites. If you've ever had an application that needs a real-time update of something, like a stock price, you'd be familiar with using a 'periodical updater'. The periodical update ajax method is like on a car trip when the kids are saying every minute 'are we there yet?'. When the dad cracks it and says 'stop asking! i'll tell you when we get there', he's basically telling them to become Comet programmers. Now there's a few ways to do Comet, i'm just going to cover one. You'll have to forgive all atrocities performed in the name of simplicity here. So here's my bare rails app. I've made a controller which has the index action, and an action that responds to the comet request. These two files [the controller and the view], that's it. The 'event' in this application is the update of a text file called 'comet.txt'. Lets look at the index page, which is the 'client' end of things. Before we look at it, i have to give credit to a PHP comet demo. But we all dislike PHP here dont we, so nobody will look it up right? index.html.erb
  <script src='/javascripts/prototype.js'></script>
  <style>body {background:#444;color:#ccc;font-family:trebuchet ms;} strong {color:#fff}</style>

  <p><strong>Comet Messages:</strong></p>
  <div id='comet_target'></div>
    var timestamp=0; // Remember the timestamp of the file that is being watched
    function comet_connect() // Start a comet request, this is where the magic happens
      new Ajax.Request('/comet/comet_request', { // Here starts an ajax call; Comet is ajax in drag
        parameters: {'timestamp':timestamp}, // Notify me when the file is newer than this
        onComplete: function(transport) { // If you will, this is the event handler or delegate
          var json = transport.responseText.evalJSON(); // Unpack the json response
          timestamp = json.timestamp; // Remember the new timestamp of the file
          $('comet_target').innerHTML += json.msg + '<br />'; // Display the response message
          setTimeout("comet_connect()",500); // Wait for the next event
    comet_connect(); // Start the first comet request
Now lets look at the comet_request action. Basically this action holds the HTTP connection open and waits for the file to change, then it sends the notification. comet_controller.rb
class CometController @json
Ok that's all there is to it. Now lets see if its going to fail in the middle of a presentation. Notice how it sends a message to the client whenever i change and save the file. Download the demo Thanks for being a great audience and listening to my ramblings If you ever decided to use Comet in a production application, use something a bit more robust than what i've described above, such as Juggernaut.

Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.

Chris Hulbert

(Comp Sci, Hons - UTS)

iOS Developer (Freelancer / Contractor) in Australia.

I have worked at places such as Google, Cochlear, Assembly Payments, News Corp, Fox Sports, NineMSN, FetchTV, Coles, Woolworths, Trust Bank, and Westpac, among others. If you're looking for help developing an iOS app, drop me a line!

Get in touch:
[email protected]

 Subscribe via RSS