check if user got new message in the background? - php

im using comet to push new data to the user.
1: but i wonder how i should check if new data (new messages, new replies etc) is available?
should i in the php in the background use a while loop and sleep it for 1 min so it can check every one min if new data has come in?
or should i have a trigger in the database for this?
what available options are there?
2: and how can i actually check eg. if user got a new message with SQL? how should i set up "the system"?

1) I would use AJAX to ask for new messages from an opened user page. This way you do not need a script running on the background and only active users/pages need the results, so let them ask for it themselves.
2) If you send the date of the last message along with the AJAX request. It is fairly easy to check if the database contains newer messages with something like SELECT * FROM messages WHERE date > $last_message_date

Related

symfony 2 notification or alert (for all user)

Hi everyone Im working on a symfony2 project,and my next step is to create a simple notification system,
when someone add insert a row in an entity, the others must be notified in the moment,
I wonder if I create a bloc of jquery code that get the date of the last created element and then do the rest work (show a notification ) I dont think is a good idea, or if there is a nice and a simple method to follow to create a notification system,
plz any idea could be helpful.
Thanks in advance!!!!
You shoul implement something like this:
Client send an ajax request with timestamp of last modification (first time sends 0)
Server compares timestamp of client to timestamp, to retrieve all messages with bigger timestamp than the one sent by user
If there are newer messages, return them immediately to the client, with the timestamp of the latest one On other hand, if there are no new messages, enter into a 2 minutes busy-wait loop, checking every 1-3 seconds (randomly) whether there are new messages.
When client receive servers answer, browser updates view and immediately sends a new ajax request.

Most efficient method to check whether user has new mail?

I have a simple and generic internal messaging service on one of my PHP/MySQL sites, with a unique messageID, from, to, subject, message, time, and isRead.
In general, what is the best way to check that a user has new mail?
Should I store a simple binary trigger on the main Users table that gets switched to 1 when anyone sends them a message, and then have the user check for that 1 on every pageload, and if there is one, alert them to the new mail (and set it to 0 whenever they go to their Inbox)? But what if it was spam and we deleted it before the user read it?
Or should I store the messageid of the last message the user read, and then do a check for the latest message and see if it's more recent than the last one that he read, and if so alert him? But then how and where should I store this info?
Is there another, more efficient method considering we would have to check for new messages on every pageload?
If the user goes to his Inbox, it should no longer show him that he has "New Mail" for any of the messages that were in his Inbox at the time he checked it, regardless of whether he's actually clicked to read them or not.
Thanks!
EDIT: Some of my users access my site from very basic phones that don't have cookies or even Javascript, so please keep that in mind.
Best way would be push notification from server, like stackoverflow does, using html sockets.
jquery plugin
But keep in mind that is not supported by all browser, so will need to fall back to ajax polling.
About spam i would suggest only notify user after spam checking if done, if possible.
Your solution to store next to user with set bit sounds right, (also you could store number of new message, instead of bit)
In your users table, store the count of new messages, and update when needed.
AJAX polling can be expensive in serverside (the select count(*) from ... can be expensive when your DB became large, and you need to do it, for example, one check per minute).
If the user just browsing in your website, and have no new messages, you can just skip select more information about messages.

How to do notifications in PHP?

How can I show users that they have a new message or response awaiting in their inbox by showing them a number or text telling them to check it and then only remove the notification automatically if the user responds to it or clicks "done" (meaning no further response necessary).
I assume the messages will be stored in a DB. Just add a 'read' boolean column and then query the DB for unread messages to know whether there are new messages.
When the user replies you set the read column to 1.
This really needs javascript. Basically you have javascript make a request every x amount of seconds to a php page that checks for new notifications. Then the javascript alerts the user.
if you wan't something like on facebook, and other social network sites, U need to use javascript to do this. Php is serverside language, and that can;t do.
if you don't ask that, and u are only interested how to solve this with php and database.
Try to make new row. Called status, and update table set that coloumn is 1 if is read, 0 if isn't read...

How can I build a PHP/MySQL live chat?

I want to learn how to make one of these systems from scratch and I'm find a lot of junk links on Google. I really just want a simple tutorial for the most basic PHP and MySQL chat so I can understand the concept before I start messing with jQuery/AJAX.
PHP/MySQL chat 101:
1) user opens a browser
2) user enters address in brower
3) browser sends HTTP request
4) server recieves HTTP request
5) server tells PHP interpreter to run PHP script
6) PHP script connects to MySQL database
7) PHP script retrieves list of messages
8) PHP generates HTTP response made of HTML code with messages and form
9) Server sends HTTP response to browser
10) Browser draws HTML from HTTP response
11) User types new message and submits the form
12) Browser send HTTP POST request
13) ...
A very simple starting point
Have a database table for a Message
id | user | timestamp | message
And have a PHP page that sends an AJAX request to read any new messages.
This will involve checking the database to see if there are any messages since the time the request was received. If no messages, then loop, wait and try again in 100ms (or whatever you think is acceptable lag).
When the Ajax request returns a message (a JSON response would be best), output the user, time and message to the page using JQuery.
The live part of your chat is the tricky part, if you are just beginning i would skip that.
Start by building a simple guestbook, and then add more features.
There are many tutorials available on how to build a guestbook, and even some free scripts where you can learn from.
After you got your guestbook working, you could add features like auto-loading new messages to make it appear as live, using AJAX polling. What you basically do make an AJAX call to the server at a regular interval to get all the messages and display it on your page.
Found very interesting tutorial here
http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/
If you must use php and mySQL for chat, atleast have a seperate table for unread messages. If you poll you will most likely need to check the database for new messages every 100ms or so. If your total message table is 1000 rows checking every 100ms will kill your server (especially if many users are connected). I would structure my mySQL database with a table for only unread messages and move them to a bigger table for old messages once read. That way your not checking a big table all the time.
Even better is to use a cache database for unread messages like redis (facebook used memcacheD).
Even even better is to just not use php all together and use an event driven language with callbacks like node.js

New message popup

I currently have a messaging system.
I am looking to make it so that when a user receieves a new message, they get an alert. A popup perhaps? Like a message box saying "you have a new message".
How could I achieve this?
Thanks
You can't really do this in php, as normal using of php implies no direct user interaction. What you need to do is to implement the relevant user interaction code in for example javascript, and do polling towards the server hosting the php (using AJAX or similar), to see if an popup should be shown or not.
This is not only a php problem. You need to make a database table like this:
messageID
contents
...
popuped
userID
Then make a page who gets the count of messages who are not popuped already:
SELECT COUNT(*) AS nr FROM messages WHERE userID = :userID AND popuped = 0
And after that set popuped to 1:
UPDATE messages SET popuped = 1 WHERE userID = :userID
And print the nr field.
Then for every page on your site you need to make a scheduled ajax request (for example every 20 seconds) who loadeds that page. If the result is higher as zero you need to show that the user has messages.
For the ajax request take a look for example at the mootools javascript framework (or jQuery, Prototype, etc.)
Are you looking for a 'live' message, as in an alert displays while the user is doing other things? Or are you looking for a 'static' message, as in an alert that displays when the user opens a page? The static method can be achieved with server-side php. The live method will need to employ client side techniques. I recommend using jQuery and jGrowl and something like jQuery Timers for the live methods. Try this SO question about javascript timers here

Categories