Model PHP/Ajax Notification System? - php

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.

Related

Updating a changing Friend List that currently only gets called from an Activity's onCreate?

I'll try to keep this as simple as possible. Bear with me as I'm still quite inexperienced with Android.
I've created a side panel drawerLayout that displays a Friend List for users of my application. This includes sent requests and received requests:
So far, every time the activity is created, a PHP request to the remote server is populating this list with friend of all status types, like so:
MyAdapter adapter = new MyAdapter(this, populateFriendList());
I'm going to have buttons beside each friend in the list that can accept requests, decline them, etc. As an example: when confirming a friend, I've been able to remove them from the 'Sent Requests' section of the list view, and even update the database to reflect this.
But how am I supposed to update the ListView to then see the most up-to-date friendList, with the newly confirmed friend as a friend?
Worth noting is that rotating the device causes the friend list to update as the onCreate method is called again, meaning populateFriendList() gets called once more. I appreciate that this way probably isn't wise, but I'm not sure what I'm supposed to do instead.
There are a couple ways to handle this. You could use GCM to send a push notification when there is a new friend request, or you can do long polling in a service pinging your server every interval to see if there is a new request.
your rotation is calling onCreate() every time because thats what's happening in the activity. You can override OnConfigurationChanged() and that should handle rotation changes.
You can call onNotifyDataSetChanged() to let the adapter know there there is new data to be viewed.

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.

PHP MYSQL JQUERY Feedback/Notification System for Server Events

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 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?

Storing a "Users Online" list for a chat application (php/ajax)

I have several chat rooms. Currently, I store the list of chat users in a php variable. If a user enters or leaves the room, the user's name is added/removed from that list.
To make this storage persistent, I use memcached. To update the status of the chat room and send the user list to all users in the chat room, I use periodical ajax requests that fetch the user list into the browsers of the users who are in the chat.
It works okay. But I doubt that sending the whole list of chat users to everybody every XX seconds is a good idea if there are a couple of hundred people in the chat.
How are chat rooms usually dealing with this problem?
Have no idea how they do, but here are some ways you could. Remember to measure before and after you optimize.
Version the list. Don't get the full list if you have an up to date version.
Diff the list. Send only the updates since the last update.
Log this list. Cache or push updates as new events occur.
The way I run my chat rooms, I timestamp all the new messages and new sign ins and sign outs.
When a user enters a room, I download the full list.
Periodically(via an AJAX with JSON call), I will download any new events (messages, logins and logouts). And update the relevant lists accordingly.
Honestly, what you are doing sounds pretty good. Keep doin what ya doin. Maybe keep track of the last list and only send an update if they have changed in the last x time? If you really want to save a few bytes.
I would push the list updates through the same channel that the chat messages go through, too. If it's an endless-loading page, maybe you could inject something like this in the page:
<script>updateUserlist({user:"alice", eventtype:"leave"});</script>
Thanks for all the suggestions. Additionally, I will look into running a comet server.
The long-polling approach just would not scale well.

Categories