title: Autorefresh a .gsp in Grails when a database table changes
body: <p>What I have:
   - A database table called 'orders' that is constantly populated by some java module on the back-end.
   - A website running on Grails that displays those orders. More precisely - a list.gsp in the Orders view.
   - the list.gsp will display new orders if the refresh button is pressed on the browser.</p>
  
  <p>What I need:
   - Some way for the .gsp page on a client to get refreshed automatically when a new order is placed on the database.
   - The autorefresh needs only to autorefresh the .gsp page when the client is already in the .gsp page. i.e. if a client is on the show.gsp for a particular order, then no need for autorefresh.</p>
  
  <p>Things I though might help:</p>
  
  <ul>
  <li><p>option1: Having a grails service that will periodically (every 5sec.)  query the database to see if there are any new orders. If there are, then somehow from the .gsp page call again and re-render the .gsp page ??!</p>
  
  <pre><code>  - suboption1-1: create and destroy a new connection every 5 sec.
    - suboption1-2: create and keep a connection alive ... forever
  </code></pre></li>
  <li>option2: Have the java module that places the orders call a refreshController thru the web and somehow re-render the .gsp page. i.e. Have the refreshController notify all clients currently in the .gsp page that a refresh is needed.</li>
  </ul>
  
  <p>=================================================================================
  Follow up:</p>
  
  <p>How can I call a controller from java script?:</p>
  
  <pre><code>function checkDB()
  {
     t = setTimeout("com.mypackage.DBChecker.checkdbController.checkAction()", 5000)
  }
  </code></pre>
  
  <p>==================================================================================
  Follow up 2:</p>
  
  <p>So I almost got what I wanted working except that I can't figure out how to AJAX back to my list.gsp page only part of itself. I dont want to refresh the whole page, but only a division with id="refresh table". i.e. . </p>
  
  <p>I have the following code at the moment that is not working:</p>
  
  <pre><code>   &lt;script type="text/javascript"&gt;
          function checkDB()
          {
              var xmlhttp;
              var xmlDoc;
              var x;
              if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
              }
              else
              {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
  
              xmlhttp.onreadystatechange=function()
                {
                if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
                {
                    xmlDoc=xmlhttp.responseText;
                    x=xmlDoc.getElementsByTagId("refreshTable");
                    for (i=0;i&lt;x.length;i++)
                    {
                    txt = x[i].childNodes[0].nodeValue;
                    }               
                    document.getElementById("refreshTable").innerHTML=txt;
                }
                else
                        {
                        }
                }
  
              xmlhttp.open("GET","http://localhost:8080/Orderlord/checkdb/checkdb",true);
              xmlhttp.send();
          }
          window.load = checkDB();
      &lt;/script&gt;
  </code></pre>
  
  <p>=================================================================</p>
  
  <p>Follow up 3:</p>
  
  <p>I succeed in returning only a partial page through my ajax call by copy/paste the div that i wanted to a separate gsp. I dont know how It is working - it's a bit of a magic to me, but everything works, except for when I try to sort the columns of my table. Then, only that partial gsp is rendered on my browser, without the rest of the initial page resulting, in a simple tabulated text page. Also once I end up in that kind of html page - the autorefresh doesnt work anymore, but as long as I will stay in the checkDB list view, the page will refresh every 5 seconds. </p>
  
  <ol>
  <li><p>...So , how do I fix my  problem</p></li>
  <li><p>Another thing i cant figure out is how to return True/False from a controller to a javascript function in the gsp and how exactly did you have in mind for me to use it?</p></li>
  <li><p>And lastly, I am currently using 'window.load = timeDB' inside my  tag to call the timer function. I tried using , but for some reason I cant get it to work no matter what. Is there something I should keep in mind when using ?</p></li>
  </ol>
  
  <p>Very lastly: What can I do to simply refresh part of the gsp every 5 sec?</p>