PHP MYSQL JQUERY Feedback/Notification System for Server Events - php

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

Related

turn based game with ajax and php

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.

In PHP, how to keep a process running to monitor for an event?

I have a DBMS assignment to create an entire online bidding website that allows users to do many things like bidding on a timed auction, buy-it-now based on timed availability, and search among MANY more things. I've completed the website, with one minor exception: once a user wins a timed auction, that user must be notified that s/he is the winner and given 24hrs to checkout the item.
My early approaches consisted of simple PHP scripts that queried the DB when the user was on the page to see if the bid time ended and to see who the winner is, but there are two things wrong with that approach. 1. The user has to constantly refresh to find out if s/he won -- if they're on the page shopping. 2. If the user doesn't log into the site for a month after the auction ended, s/he will not know for a month that s/he won the auction and therefore misses the 24 hr window to checkout.
I've been reading about cron jobs, but my concern is that, since they are scheduled jobs, there are no convenient times to schedule the jobs so that they get processed efficiently. What I mean is that I can't continuously monitor and let every user who wins know instantly after the auction ends.
I also read on PHP exec( ), but I'm not sure that it's the tool that I need to run this task because once exec( ) finishes, it doesn't re-run.
So I guess, my question is if anyone has an idea of what I could implement to help me with this task.
You should use cron - have it run every minute or so to check the DB to see if some auction ended. The greater problem I think you're construing is the notification to the user. If its the case they may not be on the site (or on some random page when they win) then don't rely on notifying them via the website and certainly not from a PHP request they may or may not make to the site. Instead, consider having the PHP called by cron to check for winners send an email to them to notify them.
Think about Facebook (only because given the user base its the most universally experience example) - they provide a notification area if you're on the site, but if you're not on for a while, they send you an email letting you know something has happened. This is what you should do.
You should reconsider using cron scheduling, you can run jobs at very fast intervals.

I need to show which users are online for my AJAX Chat

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?

Generate a list of online users?

I'm not awesome enough to write a chat application, and I'm trying to get one to work, and I've recently downloaded one from here, it's pretty good so far, as I've tested it out on XAMPP, but I have a slight problem. I'm trying to generate a list of online users to give it a more practical application-like feel, but the problem with that, is I have no clue how to do it easily.
When users login to my site, a session named g_username is created, (the chat says 'username', but I'll fix that) and from what I see so far, the easiest method would be to store their username in a database called OnlineUsers and call that data via Ajax, but, the other problem, is that it's session based, and sometimes the users can just leave, without logging out, and I intended to run a script to logout the user from both the OnlineUsers table, and by deleting the session.
If they leave without logging out, they'd be online forever! I could potentially suffix a bit of code on every page, that toggled an ajax event on page close, the event being a script that kills their OnlineUsers table record, but then again, that would load the server with useless queries as users jump between pages, as far as I'm aware.
Creating my entire site in Ajax isn't really an option, as it's a load of different sites combined in to 1 'place' with a social 'layer' (if you will) from a social service.
Does anyone see a way to do this that would make sense, and be easy to integrate, and do with Apache, without command line access?
You could so something like storing a timestamp of the users last action in a database, comparing that timestamp when outputting online users and making sure that it was done at most 1 min ago.
Run on all/vital pages:
(Deciding if the last action is outdated, you could also check if it was done for one minute ago to reduce the database-load)
if($user['lastAction'] < time()) {
//update into database, last action is outdated
}
When calculating the amount of users online and is within the loop of each timestamp
//If the users last action was within a minute, the user is most likely online
if(($row['lastAction']- time()) > 60*60)
//count user as online
you could have a cron job [if you have cpanel] running on the server once every 60secs or so, that checks when a user last sent anything via the chat if they have not in the last lets say 5mins then remove their entry from the online users list.

Model PHP/Ajax Notification System?

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.

Categories