Search code examples
javascriptjquerydesign-patternsauto-update

Constantly Querying Server via Javascript - Good Idea?


I've got a small website that has about 5-10 administrators. I've set it up to monitor what each administrator is doing (adding items, removing items, etc). I had a list within our admin-panel that shows the previous 10 activities performed by the collective administration. Today, I decided to make this self-updating every 30 seconds.

My question is simple: is there any problem doing this? I'm calling a small bit of text with each request, and the request is likely only running on 3 or 4 computers at a time (reflecting the number of concurrent-administrators logged in).

  $(document).ready(function(){
    setInterval("activity()", 30000);
  });

  function activity() {
    $("#recent_activity").load("../home/login #recent_activity .data");
  }

Produces the following (or similar - only with 10 rows) with each request.

<table>
  <tbody>
    <tr>
      <td><p>jsampson</p></td>
      <td><p>logged out</p></td>
      <td><p>28 minutes 27 seconds ago</p></td>
    </tr>
    <tr>
      <td><p>jdoe</p></td>
      <td><p>logged in</p></td>
      <td><p>29 minutes 45 seconds ago</p></td>
    </tr>
  </tbody>
</table>

Solution

  • 3-4 users every 30 seconds isn't very much at all. Even 300 users at that rate wouldn't be much at all.

    You may want to check into these questions:

    You can cache this as well, and it would be advisable especially if the query to generate the page is computationally heavy, but of course take into account what kind of lag you want in the most recent content being displayed.