Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 821
Sampson Avatar asked Jun 22 '09 15:06

Sampson


4 Answers

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:

  • Should I use Ajax Push or Pull
  • Simple Long Polling Example Code

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.

like image 63
cgp Avatar answered Nov 07 '22 09:11

cgp


You should cache this and only update the cache every 30 seconds.

like image 42
Josh Avatar answered Nov 07 '22 11:11

Josh


No, there shouldn't be any problem at all. I do the same thing at 1 minute intervals for a notification system I wrote on my company's intranet portal. Any web server should be able to handle this, honestly.

It's really no worse (in fact, significantly better) than them, say, refreshing their browser every 30 seconds... Considering the significantly smaller data transfer, it would likely be something on the scale of 10-20 times better than refreshing it... or, about the same bandwidth of refreshing once every 5-10 minutes. :-)

like image 1
KyleFarris Avatar answered Nov 07 '22 11:11

KyleFarris


No issue at all in my opinion. Sites that run on a much bigger scale (Betfair for instance) use hundreds of xhr calls per minute per connected client. Obviously they have much bigger hardware infrastructures but the browser copes fine.

I have sites that use a smaller interval and they scale to a few hundred concurrent users running from a sinlge webserver.

like image 1
redsquare Avatar answered Nov 07 '22 09:11

redsquare