title: Is there a way to 'listen' for a database event and update a page in real time?
body: <p>I'm looking for a way to create a simple HTML table that can be updated in real-time upon a database change event; specifically a new record added.</p>
  
  <p>In other words, think of it like an executive dashboard.  If a sale is made and a new line is added in a database (MySQL in my case) then the web page should "refresh" the table with the new line.</p>
  
  <p>I have seen some information on the new using <code>EVENT GATEWAY</code> but all of the examples use Coldfusion as the "pusher" and not the "consumer".  I would like to have Coldfusion both update / push an event to the gateway and also consume the response.</p>
  
  <p>If this can be done using a combination of AJAX and CF please let me know! </p>
  
  <p>I'm really just looking to understand where to get started with real-time updating.</p>
  
  <p>Thank you in advance!!</p>
  
  <p><strong>EDIT / Explanation of selected answer:</strong></p>
  
  <p>I ended up going with @bpeterson76's answer because at the moment it was easiest to implement on a small scale.  I really like his Datatables suggestion, and that's what I am using to update in close to real time.</p>
  
  <p>As my site gets larger though (hopefully), I'm not sure if this will be a scalable solution as every user will be hitting a "listener" page and then subsequently querying my DB.  My query is relatively simple, but I'm still worried about performance in the future.</p>
  
  <p>In my opinion though, as HTML5 starts to become the web standard, the Web Sockets method suggested by @iKnowKungFoo is most likely the best approach.  Comet with long polling is also a great idea, but it's a little cumbersome to implement / also seems to have some scaling issues.</p>
  
  <p>So, let's hope web users start to adopt more modern browsers that support HTML5, because Web Sockets is a relatively easy and scalable way to get close to real time.</p>
  
  <p>If you feel that I made the wrong decision please leave a comment.</p>
  
  <p>Finally, here is some source code for it all:</p>
  
  <p><strong>Javascript:</strong></p>
  
  <p><strong>note, this is a very simple implementation.  It's only looking to see if the number of records in the current datatable has changed and if so update the table and throw an alert.  The production code is much longer and more involved.  This is just showing a simple way of getting a close to real-time update.</strong></p>
  
  <pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" charset="utf-8"&gt;
  
  var originalNumberOfRecsInDatatable = 0;
  var oTable;
  
  var setChecker = setInterval(checkIfNewRecordHasBeenAdded,5000); //5 second intervals
  
  function checkIfNewRecordHasBeenAdded() {
  
          //json object to post to CFM page
          var postData = {
          numberOfRecords:  originalNumberOfRecsInDatatable 
          };
  
          var ajaxResponse = $.ajax({
          type: "post",
          url: "./tabs/checkIfNewItemIsAvailable.cfm",
          contentType: "application/json",
          data: JSON.stringify( postData )
          })
  
          // When the response comes back, if update is available
          //then re-draw the datatable and throw an alert to the user
          ajaxResponse.then(
          function( apiResponse ){
  
           var obj = jQuery.parseJSON(apiResponse);
  
           if (obj.isUpdateAvail == "Yes")
           {              
              oTable = $('#MY_DATATABLE_ID').dataTable();
              oTable.fnDraw(false);
  
              originalNumberOfRecsInDatatable = obj.recordcount;
  
              alert('A new line has been added!');
           }
  
          }
          );
  
      }
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Coldfusion:</strong></p>
  
  <pre><code>&lt;cfset requestBody = toString( getHttpRequestData().content ) /&gt;
  
  &lt;!--- Double-check to make sure it's a JSON value. ---&gt;
  &lt;cfif isJSON( requestBody )&gt;
  
  &lt;cfset deserializedResult = deserializeJSON( requestBody )&gt;
  
  &lt;cfset numberOFRecords = #deserializedResult.originalNumberOfRecsInDatatable#&gt;
  
  
  &lt;cfquery  name="qCount" datasource="#Application.DBdsn#" username="#Application.DBusername#" password="#Application.DBpw#"&gt;
      SELECT COUNT(ID) as total
      FROM myTable
  &lt;/cfquery&gt;
  
  &lt;cfif #qCount.total# neq #variables.originalNumberOfRecsInDatatable#&gt;
      {"isUpdateAvail": "Yes", "recordcount": &lt;cfoutput&gt;#qCount.total#&lt;/cfoutput&gt;}
  &lt;cfelse&gt;
      {"isUpdateAvail": "No"}
  &lt;/cfif&gt;
  
  
  &lt;/cfif&gt;
  </code></pre>