I develop a turn based game and want to put up to browser to test with real users. because it is a turn based game I don't need it to be real time. my idea is using old school chatroom method to set text to communicate between 2 players. after much testing and reading, I found this site provide a relative decent guideline and I tested it on my server. However there are 2 problems that I don't know how to do:
How do I keep the communication between the 2 specific players? I imagine if this is chatroom, every time a player start a game, it is just like start a private chat room and only allow another player to join in. How can I do that? I need to understand the methodology to be able to work around the code.
if ever any of the player offline, how can I ping the other online player?
Your question 1, seems to be a matter of validation.
You should just validate that a game is on-going between user1 and user2.
Assuming you have a game table, you just restrict access to the page.
You should probably have a chat-game tablet as well, if you're keeping history.
Your question 2, you have a page that is constantly flushing and updating the chat, you have access to the user session\cookie. Seeing as this is a 2 player-game, when you send out information, you set a flag, of last-received per used, if it's between longer then a minute, between one user receiving, just update the response, so that the user gets that information as well.
When the user comes back, just grant him access to the page again, using gameid, or something of sorts.
Actually not that hard.
Related
I am in an infrastructure guy so forgive if the question sounds naive. I need to work with a dev partner in capturing the following data from a PHP web application.
Time of entry
Device, OS, Browser
Network, source of the visit
User actions on site, speed of scroll
Current page the user is on
The idea is to pump all this data to a data store to run machine learning algorithms to come up with near real-time recommendations. I would like to understand to what extend does a standard cookie can track this data. Is it possible to collect all of this data?
What is the right way to go about collecting this data?
Everything about the user can be stored in a database for future reviews which is the best way to keep a track of user on what he does on the website.
To give a few ideas,
1) Time of entry ==> When the user enters the website just, having a
TIME_STAMP field will do the trick, which will automatically update
the user's entry value.
2) For detecting OS => just use var_dump(PHP_OS);
3) For detecting browser,device => $_SERVER["HTTP_USER_AGENT"]; will
do the trick
4) Current page the user is on => This can be stored in a $_SESSION
variable and it can be kept updating every time the user goes to some
other page and store that too in the database
5) UserActions => Depends on what actions you want to capture and the
same can be updated in the database as well.
I need to build a notification system for a website that gets traffic from at least 5,000 users at the moment.
The users win points performing certain activities. This activity gets recorded to a database table.
The problem is, it's not always an AJAX call to the server that does it. One of the items is an iFrame photo upload. Plus, not every user is enrolled in points, so I want to avoid building code that looks for these things when updates occur, so I don't have to write alternative code for users not enrolled. Lots of these things are normal actions that any user would do, enrolled or not.
I want to show a div on the page describing the reward the user just did. It's similar to the way XBOX Gamerscore points are shown when awarded.
My best idea thus far is to run an AJAX call to the server at an interval looking for non-alerted items. But I want to keep it light, like once every 10-15 seconds.
Any other solutions for this? I'm running this off a shared hosting environment, so I won't have the ability to do crazy things with the server nor be able to do long polling.
Thanks!
Clint
I have an inline chat application which I got from Ajax Chat, which is working brilliantly. The application allows a user to chat with users that are registered on the system. Ie:
Now I need to show if the user is online or offline.
So my question is how do I show online users using PHP?
Thank You
Basically what you need is a way to register users activity.
One way you can do this is doing it by sessions within PHP, and you log these. There are tons of ways to register then your activity in a log. If the activity is not updated for example in 5 minutes, the user is offline. Bassically you just need then a sessionId, and a timestamp (and i would recommend this also to hang to a userid). If offline, there is no userId assigned and when online you add a userId. If you have those, its pretty easy. Its a matter of updating them constantly when a new page is loaded and if they log out, you simply destroy the session, or update it so it wont be linked to the user.
It may not be the best system, but it works, and it might help you.
I don't know your specific needs. Pardon me, If I am wrong.
If Jabber support is there with Ajax Chat, why not try ejabberd kind of XMPP servers rather than re-inventing the wheels on your own. And you could have a look at Apache Vysper too, since it has support of extension modules too. If XMPP server is there, users presence handling and message transfer would become a cake walk.
What you need is a constantly update for a table in your database that save the last change in an user and save the date time... so if that date is more than 5 or 10 min, the user ir off..you can do it with ajax...
What i would do is have a script that the clients run to do an ajax call to update a entry in your database with a time stamp for last seen. Not too often or you will overload your server.
you can also put some if statements where it checks for keystrokes, mouse movement, and if the window is active if you really want to get technical and do a away status.
then in active chats just check the time stamp for active messages or when the user list is open. anything outside a acceptable range will show the user as off line. 5 minutes seems pretty long to me. poll for a check every 10 seconds maybe?
I'm making a website similar to Facebook - with things such as Notifications which make it similar. I don't really see how I can get notifications working. Well, I figured out how I could get them from the database, with a query.
The structure I think the site will follow would be multiple tables for different applications - ie: Photos would add in an ID for the picture, a filename, and a few user IDs if 'tagged' or something, and how to send that information to the user in real time.. is beyond me.
So I would have to run several queries every few seconds scanning the database tables for the $_SESSION['id'] of the user being found in all the applications tables with a status of unread?
Another possibility is that every user has their own table? That's .. a lot. lol.
Or just a notifications table with the most recent notification being pushed to the table with a unique id and a user id?
I really can't wrap my head around this, lol.
Also, displaying notifications in real time? I understand Facebook uses long-polling and gets the notifications in real time, but I don't think I could leave about 5-10 queries (for each app) running on a long poll for multiple clients, or that'd completely crash my server, right?
Any advice/code on how I could try and make a notification system for a social networking-ish site? If not, I think i'll go with static notifications rather than any sort of realtime.
Then again, that'd be too much load querying the server every few seconds for a new notification on every page load? Using ajax would mean long polling, so it's a lose-lose.
I would say Long polling is the answer. Gmail and Facebook both use this method for real-time notifications. Your only other alternative is Flex with a dataservice, but that is not PHP.
In terms of performance, the query is only going to pull from 0-5 notifications at a time, and if the tables are indexed properly, and the query is written well, then 5 of these queries will not be a significant impact on your server.
Furthermore, if Gmail and facebook are doing it, then it stands to reason you can also do this. Granted, they have a ton of servers to support all their users, but I am going to go out on a limb and say you don't have as many users as they do, so as a result the server technology will work for now. And when you get so many users your current servers can't handle the load, then you invest in newer more powerful ones.
Well here is my take on it.
You could create different tables for status, photos and videos.
Everytime somebody comments on a video or something you can do the query to store the notification along with the information of the user who liked it, you should set a status field too, so you can query based on which has been seen and which has not been seen by the user.
You can put the url of the page where the photo is or status is located so when the user is logged in you do a query every five minutes checking for unread notifications, if there are any you display them in a tiny toast message on left bottom side of the screen like facebook.
On click of the toast message you can do an ajax call to update that status of the notifcation to read so it does not show up again and in the success call back you can take the user to the page where the status update is.
We have a jquery/php/mysql system that allows a user to log in and review videos built by a system for online viewing. When a user begins reviewing a video, the video is marked as such. But now we've cornered ourselves into the classic browser-based application problem of the user navigating away or closing the browser without completing review. That video would then enter a state of limbo of constantly being reviewed, but never completed, and never re-entering the queue.
Options we have are:
Build a service (which we already have others) to find review sessions that are outside a duration boundary and reset them back into the queue.
Reset review sessions outside a duration boundary when that user logs in. Essentially, if a user locks out a video for review, it'll be unlocked the next time they log in.
A suggestion made to me was to use the php/apache session length and on expiration, reset any pending review jobs. I don't even know where to look to implement this as this is one project on a shared server, so it shouldn't be an apache config, but the reset mechanism would need to know the database credentials to be able to reset it...
The worst solution everyone hates is preventing the user from navigating away with javascript, asking "Are you sure?!"
This system is used by a few hired reviewers, so I'm not exactly dealing with the public here, but I can't prevent users from sharing logins for speedier review, which would knock out the 2nd option above because it would unlock a video being reviewed by someone else using the same login.
There are two good options that won't tax your server. Either:
Run a cron-job every hour looking for review sessions that are outside of the duration boundary. This has the advantage of being transparent to the end user. But it's possible to kill an active session if you're not careful (Suppose the user is operating in multiple tabs).
Prevent users from navigating away with JS. Honestly, this is what I would do since the user is reviewing the video (if they were just viewing it, that would be bad, but since they know they are supposed to be acting, it's ok). Just say If you leave now, the review will be canceled, are you sure?.
Honestly, I'd do option #2. SO uses it, and it works well here. It wouldn't be for every page, just those where there's an active review going on...
When a single video is reviewed on a single page by one person. You can capture the JavaScript unload event which will be fired when the page unloads and the client is leaving the page. Than you can change the state of that video or even show a dialog and let the user decide.
Maybe a ping-pong system may help. In the back you make AJAX calls binded to a video by an ident. When that activity stops, the users stopped. A background process can look for latest ping and when that is, for instance, a hour ago, change it's state.
Your first option is your best bet. You can have a javascript timer on the page updating the lastestActivity record for the video while the browser window is open So you won't get into the corner of a review that takes too long and the unlocker releases the lock prematurely.
Option 2 is problematic for several reasons, some have already been mentioned but taking into account that a reviewer might not log in for a few days (vacation?) will unlock a video for too long unnecessarily.
Option 4 (Javascript onBeforeUnload) Won't cover you in the common cases of a browser crash, OS crash or a power failure. but it is something you can implement in addition.