i use the following script to start a long poll with the php file.. it checks if any results are updated and sends a response ..
For some reason when this javascript is inserted all other scripts hangs on a long poll on fire bug
function waitForMsg(){
$.ajax({
type: "GET",
url: "auth/classes/getdata.php",
async: true,
cache: false,
success: function(data){
console.log(data)
setTimeout("waitForMsg()",1000);
},
error: function(XMLHttpRequest,textStatus,errorThrown) {
// alert("error: "+textStatus + " "+ errorThrown );
setTimeout("waitForMsg()",15000);
}
});
}
$(document).ready(
function()
{
waitForMsg();
});
This is the php file getdata.php
require_once($_SERVER['DOCUMENT_ROOT'].'/auth/config/db.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$user_id = $_SESSION['user_id'];
$lastmodif = time();
$update = 1;
while ($update <= $lastmodif) {
usleep(10000);
clearstatcache();
$sql = "select ua.user_id as member,ua.post_id,pa.user_id,pa.type,pa.time,CONCAT(u.first_name,' ',u.last_name) as
name,u.thumbnail from user_activity ua right join post_activity pa on
ua.post_id=pa.post_id right join users u on pa.user_id=u.user_id where
ua.user_id=".$user_id." and pa.time > FROM_UNIXTIME('".$lastmodif."')";
$result = $conn->query($sql) or die(mysqli_error());
if ($conn->affected_rows > 0) {
$update=$lastmodif;
$response = array();
$response['msg'] ='update';
echo json_encode($response);
}
}