I have a shoutbox which is coded in PHP and Javascript and uses MySQL.
It functions like this.. We have a refresh.txt file that checks for new shouts, if there is a new shout there is a call to the database to grab the new shouts.
Now as we all know the main problem with shoutboxs is the CPU usage.
So I was wondering if there was a way to count active users without it adding too much extra stress on the server.
Maybe via Javascript?
Unfortunately I do not have any source code to hand as it would be a new feature added in there.
Any help would be great, thanks.
Are these users logged in with a PHP session? If so, just count the number of active session files in the session save directory. That’s not CPU-intensive and involves no database calls.
Further, if you’re still avoiding database calls for this, you could have a private file keep track of the different clients (e.g. IP addresses) and when they last requested a resource from the server. More on this in this question.However, if multiple users visit from the same network (e.g. schools or businesses) then you have a logical error, and the solution is to log individual users with cookies or sessions.
You may also be interested in long polling (Javascript). It keeps a connection open with the server and occasionally checks to see if there’s new data. This can be useful when counting the number of active users without, per-se, sessions or cookies.