1

I'm working on an ajax long polling function with mysql and it doesn't really seem to work. My computer is overheating and the website crashes after some minutes. Also, the poll.php doesn't even receive the content from data.php, instead, poll.php shows {"type":"connect_error).

I have not done any long polling before.

I have 3 files:

data.php

<?php

session_start();

define ('DB_HOST', 'localhost');
define ('DB_USER', 'root');
define ('DB_PASSWORD', 'root');
define ('DB_NAME', 'story_creator');

function sqlSelect($query) {

// Create connection
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$result = mysqli_query($conn, $query);

// Check connection
if (mysqli_errno($conn)) {
    echo "Failed: " . mysqli_error($conn);
}

$resultArray = array();

if ($result) { 

    while ($row = mysqli_fetch_array($result)) {
        $resultArray[] = $row;
    }
}

return $resultArray;

}

$news = sqlSelect("SELECT type FROM users_news_feed WHERE user_id =               {$_SESSION['user_id']} AND date > 0;");

echo json_encode($news);

?>

poll.php

<?php

$filename = 'data.php';

$lastmodif = isset($_POST['timestamp'])? $_POST['timestamp']: 0;
$currentmodif = filemtime($filename);

while ($currentmodif <= $lastmodif) {
    usleep(10000);
    clearstatcache();
    $currentmodif = filemtime($filename);
} 

$response = array();
$response['type'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);

?>

Javascript

<script>
    var timestamp = null;

    function waitForMsg(){
    $.ajax({
    type: "POST",
    url: "functions/poll.php",
    async: true,
    cache: false,
    timeout: 50000, /* Timeout in ms */
    data: { 'timestamp': timestamp },
    success: function(data){
    // if(data == ''){
        // Just console.log the data
        console.log(data);
    setInterval(
        waitForMsg(), /* Try again after.. */
        1000); /* milliseconds (15seconds) */
    // }
    },
    complete: function(){
        setInterval(waitForMsg(), 7000);
    }
});
}
$(document).ready(function() {
    waitForMsg();
});
</script>
Peter Sall
  • 47
  • 8
  • where is the file that uses the javascript – Drew Aug 30 '15 at 18:08
  • Currently in the site's footer – Peter Sall Aug 30 '15 at 18:16
  • if you want me to recreate it and you get no help in an hour ping me with the file info. good luck – Drew Aug 30 '15 at 18:18
  • Give the complete path to the file ```$url='http://localhost/my%20projects/longpolling/data.php'``` Then get the content using ```file_get_contents($url)``` Secondly as far as i understand ```filemtime($filename);``` gives the time last the file was modified. I am unable to grasp why you are checking that because it won't tell you whether the response of the file has changed. – Untimely Answers Aug 30 '15 at 19:26
  • 1
    Modify the url in the above comment to ```$filename='http://localhost/functions/data.php';``` – Untimely Answers Aug 30 '15 at 19:34
  • But how would I else check for updates? I can't send a request to the database every time? – Peter Sall Aug 30 '15 at 19:38
  • I have never done long polling myself but my intuition is that you will have to poll the database to check if something new has happened and return the response. Can you describe a little more about your application ? http://stackoverflow.com/questions/333664/how-to-implement-basic-long-polling. Read this answer. – Untimely Answers Aug 30 '15 at 19:47

0 Answers0