Reputation:
I am looking for the best way to keep a near real-time accounting of the number of users on my website. This is in a LAMP environment and I would like to build a solution that uses JavaScript on the front end and PHP/MySQL on the back end. My end goal here is to have a MySQL table that has a record for each active user.
My current idea for gathering that information is to simply include a piece of javascript on each page that runs an AJAX request that then creates the record for that user. In order to ensure that I have a unique record for each user, I plan on creating a PHP session for each user.
My question is: If I am creating a php session for each active user, will this cause a performance issue on a run-of-the-mill VPS if there is, for example, 1,000 connected users? Is there a better way to have a unique record for each user (i.e. accounting for users behind a NAT) without creating a PHP session?
Upvotes: 1
Views: 2369
Reputation: 14447
The easiest way i've found to get an estimated number of open sessions is counting the number of files in the directory returned by session_save_path()
, it's not exact science by a stretch but gives a good indication on how many open sessions there are without the need to change the session save handler to a database or other mechanism.
This question might prove useful: Find Number of Open Sessions
Upvotes: 1
Reputation: 1055
Unless you're storing huge amounts of data in the session variable, any standard VPS should be able to easily handle 1k sessions. PHP by default stores sessions on disk - if you feel the disk I/O is killing the speed of your script, consider writing a custom session handler to store sessions in a database such as MySQL instead to reduce I/O lag. If you're already storing a record in MySQL for each user, this is not too many steps away.
Upvotes: 0
Reputation: 18833
I would run it on a cron that updates every minute.
In your user table you can store a last active field that updates on page load. If the last active field is older than 5 minutes consider them gone and not online.
So every minute your cron counts the number of users with last active less than 5 minutes old then writes that to a simple file. Like a js file, or PHP if you prefer.
That way you can make your ajax call to that file and get a simple output instead of running queries with every request. If there is no real overhead, then the number of users should be fairly irrelevant.
It depends on your needs too. I assume you're using ajax calls cause you want the ability to increment the number live on screen and not just on every page load.
Upvotes: 0