Manolis
Manolis

Reputation: 167

PHP front end user queue

I'm building a site where the users can control a webcam to turn it left and right. Every user gets one minute of action. There is going to be a queuing system on the site that will allow one by one the users to control the camera. So my question is the following, does anyone have a suggestion on how to build this queuing system? Are there any tutorials or code I can use?

Thanks a lot!

Upvotes: 0

Views: 795

Answers (1)

MrCode
MrCode

Reputation: 64536

Have a database table to track the queue for example:

queue (id, session_id, start_time, last_update)

When users hit your page, insert them into the queue table. Use a regular ajax call (perhaps 30 seconds) on the page to poll the server to see if the current users turn is up. If the user is the first record in the table then it's his turn, so update the start_time to the current time and send your ajax response telling the browser to display the UI with the buttons for the camera movement.

When a button is pressed, verify on the server side that it is infact this users turn and his start_time was < 1min ago, before allowing the action. If his turn is over, delete him from the table so that the next user becomes the first record and gets his turn, then send a response to the browser so that it can hide the camera UI and give a message.

In addition to inserting into the queue on hitting the page, also check to see if the user that is controlling the camera has had his 1min, if so then delete his record (or could be done on the cronjob below).

Each time the ajax poll fires, update the users last_update with a timestamp. Use a cronjob or just on the server side calls to check if any of the records in he queue have a last_update that is older than a short time, e.g. 30 seconds., if any are found then delete them because these are users that are no longer on the page. This will also prevent attackers trying to fill up your queue.

On the same cronjob, check if the user who's turn it is has the start_time populated, if after 30 seconds he hasn't started, delete from the queue.

The ajax calls would make it nice and seamless, but they aren't essential, if the user has Javascript disabled you can still detect that and use a meta refresh of the whole page instead.

Upvotes: 1

Related Questions