I'm creating a notification system that instantly checks to see if there are any new notifications for an online user.
Here is my script so far:
//...include(myscripts.php)...
function notificationsCount($id)
{
if(is_numeric($id) && !empty($id) && $id>0) return mysql_result(mysql_query("SELECT COUNT(*) FROM notifications WHERE unRead='1' AND userID = '$id'"),0);
}
if($_GET['getNotificiationCount'] && $_GET[userID])
{
$current = notificationsCount($_SESSION['userID']);
$count = 0;
while($count<20 && !connection_aborted()){
$c = notificationsCount($_SESSION['userID']);
if($c!=$current && $c!=0)
exit($c);
$count++;
sleep(1);
}
exit("0");
}
Client Side:
var checkNotify;
$(window).unload(function () { checkNotify.abort(); } );
//Nav Notifications
function checkNotifications()
{
checkNotify = $.ajax({
url: "/notifications.php?getNotificiationCount=true&userID=<?=$_SESSION[userID]?>",
timeout: "30000",
cache: false,
success : function(data, textStatus, XMLHttpRequest) {
if(data!=0) {
$("#nav_notification_bubble").removeClass("displayoff");
$("#notification_count").html(data); }
checkNotifications();
},
error: function() {}
});
}
checkNotifications();
This works, however if I leave the page, the web server will not respond (on my client side), until the "notifications.php" script finishes.
What can I do to kill the script if the client leaves the page?
My Web Server does have Apache Gzip turned on - would that be waiting for the buffer to finish?
Apache have some features to support COMET I heard that. You can go for that.
Or another option is using Grizzly framework in Glassfish Application Server.
Or Another Solution is to use Lightstreamer. Guess what? If you write custom code,
they may work fine in local system but it may trouble the server too much in real time.
So I Guess the better option is to find something that is already built over a long time
research works. Because this is something more like a 'hack' instead of 'technology'.