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!
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.
Related
I am building an event booking system, once the user select a table, I am updating the MySQL table with the table-status="booking" once the payment and all the functionalities are completed I am again updating it table-status="booked".
Now the problem is, in checkout when the user realize the price of the table they simply close the tab or browser and leave and my table-status remains in table-status="booking". I need to catch this and need to update my table-status to table-status="not"
I do have the session variable for the user but when they close the browser.
you need to do php cron job. If u want do check every for example 30 mins. soo server will automaticly execut your script every 30mins and will for example delete or change status of users who didnt finished payment. You can google about cron job on google.
Basically I have a chat, using AJAX, PHP.
I let guests login by filling their name and clicking login.
It fills the session $_SESSION['guest'].
Now, when they close their browsers, or something, once the session ends, I want it to say in the chat
"The username has left the chat".
But I am not sure on how would I do this.
Any ways to do it? Maybe I could fill an array of users that were active in the past 5 minutes, if not, remove from the array and it will kick them off the chat.
Any ideas?
You could have an array of users in the backend with the timestamp of the last ping.
Then, the clients end can update the backend using ajax by sending a new timestamp every so many seconds.
Whilst your backend is being updated by one of the users, your backend can do a check on all users checking the last timestamp sent to the current time, if its over 2 minutes or something you can tell they have left the chat and output the message.
Without a direct connection to the browser, you won't be able to actively tell if they're connected. I'd suggest a ping-like solution, requiring the browser to ping the server every n seconds to keep the chat "alive".
If the browser doesn't "check in" with the server, the server assumes they've disconnected and reports back to the other participant.
That said, if you're able to dive a little deeper, Google "javascript real-time chat" and you'll have several paths to success.
Add a jQuery .unload() event to your chat client. When they navigate away from the chat it will be fired and you can use it to send the last bit of data to display that message.
Here is the documentation: Link.
Example:
$(window).unload(function() {
//send message to server informing it of client leaving
});
I want to create a bidding system where user can see the current price of items. And if any other user on any other location place a bid before me it should auto update bid in my browser.
I have read about autoupdate JS+Ajax functions but even if I place a 5 second timer to auto update the content on user's browser will it not put some extra load on server by making an ajax call every 5 second? Its a bidding system so user will have bids updating within 1-2 seconds so if i put an auto update ajax call for every 1-2 seconds it will put a lot of burden on server.
So I am wondering is there any better way to handle this type of stuff? how do twitter/facebook do update user's feeds?
AJAX or not, bidding systems always have high requests because people keeps refreshing the page to check for the latest bid information.
You can take a look and attempt long polling. Long polling a method where you "push" data from the server to the browser in response to the browser's HTTP request. It is a normal HTTP connection. This may reduce the number of requests sent from users to server, however you will still have many open and active connections between your users and your server.
You will want to look at long polling. In essence, this is how it works
On the server you need some sort of event mechanism (no probem with PHP)
Client (Browser) starts an AJAX request referencing a bidding item
Server checks for changes on the bid, if there is one, returns the request
If not, he waits for some time (minute range), waiting on an event concerning this bid
If such an event occurs, server returns the request with the info, if not he returns the request with "no bid" info
You might be able to get away with a streaming model...
Each JS client connects to the server once and keeps the conneciton open. As new events arrive at the server, they are broadcast to all the open connections in real time.
This is similar to the mechanism twitter uses to broadcast tweets.
I'm working with some existing PHP/MySQL code. I'm logging/tracking certain activities into a MySQL database. Certain access points are being logged. The number of times a user logs-into the system is counted. But, I need to also log the amount of time a user is logged-in, as well as the time the user is in a certain section of the Web site.
Since PHP is a stateless environment, for the most part, what's the best way to record the end-point(s); or when the user logs-out?
Note: I can't force the user to log out, as the browser can just be closed. Maybe I could just put up an AJAX timer that would count the minutes? If so, should I treat activities and time logged-in as different tables of information (MySQL)?
Edit: I forgot to mention we do have jQuery available.
Like you said, you can't force the user to logout, and you can't know for sure whether he's looking at your page or playing Pinball.
A solution would be an AJAX request every, say 5 minutes, to tell your application that the user is active. Unfortunately, if your user has locked his screen and went to play Pinball, you still don't know exactly what he is doing. Also, doing AJAX requests at intervals like this will increase server load, especially in a multi-user environment.
The best solution I think is to simply store the start_time of the user (when he logs in), then to update the end_time at every action he does, and with a session timeout.
Per example:
I log in at 5:00. Update the start_time to 5:00.
I browse to foo.php at 5:01. Update the end_time to 5:01.
I browse to bar.php at 5:03. Update the end_time to 5:03.
I go for a coffee at 5:05.
I come back at 5:15 and my session expired, I need to relogin.
So, you know I spent roughly 3 minutes on your application, since the last action I did was at 5:03 (5:03 - 5:00 = 3). Of course, you can't know exactly if it was 3 or 5 minutes. But you can assume, most of the times anyway, that if I don't do anything on your application (i.e.: execute a script, call, etc.), that I'm not using it.
Obviously, if you can capture JavaScript events like window close it's even better, or if I sign out manually: you update the end_time accordingly.
You need to capture two events.
The onCLose() event for the page and hook that into an ajax call back to your logging system.
The onClick() event for your logout button and hook it into the save ajax handler.
The onClose event will allow you to capture when either the tab/broswer is closed and the onCLick event is obvious.
Now this will not capture times when the browser dies, the machine loses power etc. so there will be instances where you will have gaps and those can be corrected by your login event handler and simply tag the last login event as logout out on the next login. This will however lead to outliers in your tracking of time spent logged in and you will need to statistically deal with those in your reporting.
You can use an extra PHP script that records the last activity and call it via ajax.
You can use javascript to monitor if the user is still active (moved mouse or pressed a key in the last 5 minutes etc.)
EDIT: Almost forgot the important part: your java script must make an ajax request eery X seconds.
So if there was no request in x+tollerance seconds you can consider the session as dead.
I have a flash application. I'm trying to send users text messages via php after they did certain things on the flash.
For example,
Text user 1 hour after they did
thing#1.
Text user 10 minutes after they did
thing#2.
Text user 1 day after they did
thing#3.
....
I'm thinking of setting up a table for the list of things that will trigger the text. Then have a cron job set up to check the timestamps of each user finishing those things.
Is there a better way out there for doing this?
That sounds like the approach I would take. Whenever you have an action that takes place at a different time than the web request, you need to "queue" that action up to be completed later. Then you need a script that runs (however often you want) that checks the queue (i.e. DB table) and processes the items in the queue.
You're on the right track.