Page 1 of 1

Flash-like hit counter with Ajax.

Posted: Mon Oct 08, 2007 4:16 pm
by JellyFish
Well, I'm thinking of making a Flash-like hit count with Ajax. When I say "Flash-like" I mean a hit counter that updates live, without page refreshes.

So, I'm thinking of making HTTP calls to to get the new hit count in the database. But I don't want it to be slow, and I don't want it to be a performance issue. So what I'm wondering is how to do this right.

I was thinking of making an Ajax call every 5 or 10 seconds. But then if I get 100 view within those seconds it would just jump to more 100 views. This I don't want, because I want to emphasize the point that it's moving up rapidly.

So, if make calls every 10 seconds but then with jQuery have the hit count move gradually up to the new number in a period of 10 seconds.

This sounds like a great idea. But is it a performance issue or do you have a better solution?

Posted: Mon Oct 08, 2007 7:07 pm
by feyd
I have to ask: why would you want/need such a thing?

Posted: Mon Oct 08, 2007 7:22 pm
by JellyFish
feyd wrote:I have to ask: why would you want/need such a thing?
Marketing purposes. And I want it. :D

EDIT

How does photobucket do it? The image counter at the bottom.

Posted: Wed Oct 17, 2007 3:06 am
by Josh1billion
That sounds like a very cool idea, let us know if you finish it (especially if you plan to release the source ;)).

It does sound like it could take up a lot of resources, though..

What if you did this?

Visitor visits page:
  • 1. Database is updated.
  • 2. Hit count is written as raw text to text file "hit_count.txt".. so the file will just say "383402" and nothing more, for example.
Client-side hit counter wants to update its hit count:
  • 1. Hit counter script reads the new hit count from the text file
That wouldn't be so bad, because then you're pushing the weight from database calls to just reading a text file (and the text file would be tiny-- less than 1 kb). In fact, you could get rid of the database altogether and just do it with the text file. The database would only be important if you want to keep track of IP addresses (to know the count of "unique" visitors).

Posted: Wed Oct 17, 2007 5:57 pm
by JellyFish
So making a call to the database requires a lot more then just reading a txt file? Currently I'm using one field in the database, not multiple. I have a registry database which has three columns: class, key and value. I store something like [Hit Counter] in the class and [global] for the key. The value, obviously, is the hit count. So all I do is update the value of this key in my registry table. Would the txt file be more efficient?

Posted: Thu Oct 18, 2007 7:24 am
by Josh1billion
I'm not 100% sure, but I do think it would be more efficient. Having thousands of visitors SELECT'ing the value from your database every 10 seconds would probably slow your database server down.