Close to real time notification with PHP and AJAX
<p>I know this has been asked, but I cannot figure it out. I am building a application, where user will receive notification (not real time, but close- so I don't want to use sockets). </p>

<p>The idea is following: if user signs up, his status is unverified. As soon as he completes the profile and the admins have approved, it will become verified and then I would like to display a little notification saying: "Hey, you are now verified!". </p>

<h2>What I have tried</h2>

<p>Been following <a href="https://stackoverflow.com/questions/333664/how-do-i-implement-basic-long-polling">this example</a>.</p>

<p>So far I have tried polling and got stuck on the PHP side. In my logic I would need to loop the table and check for column changes. As soon as the column changes from 0 to 1, stop the loop and then append the data in the Ajax call. Is that correct?</p>

<ol>
<li>Given it doesn't need to be real time, is this the best approach?</li>
<li>How can I improve the Ajax function to only append the status when there is a change of the column?</li>
<li>In PHP, do I need to include a timestamp to check the data, and do I need <code>sleep()</code>?</li>
</ol>

<h2>JS</h2>

<pre><code>function addNotification(type, msg){
    /* Simple helper to add a div.
       type is the name of a CSS class (old/new/error).
       msg is the contents of the div */
    console.log(type + msg);
}

var notiCount = 0;
function vaitForNotification(){
    /* This requests the url "notifications .php" When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: "PHP/notifications.php",
        cache: false,
        timeout:50000, /* Timeout in ms */
        success: function(data){
            if(data.verification_status[0].verification_status == "1"){
                addNotification(data.verification_status[0].verification_status);
                console.log("verified");
            }else if(data.verification_status[0].verification_status == "0"){
                addNotification(data.verification_status[0].verification_status);
                console.log("not verified");
            }
            setTimeout(vaitForNotification, /* Request next message */
                       10000); /* ..after 10 seconds */
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            addNotification("error", textStatus + " (" + errorThrown + ")");
            setTimeout(vaitForNotification, /* Try again after.. */
                       15000); /* milliseconds (15seconds) */
        }
    });
};

vaitForNotification(); /* Start the inital request */
</code></pre>

<h2>PHP</h2>

<pre><code>&lt;?php
header('Content-type: application/json');

require_once '../../PHP/class.user.php';
$user_home = new USER();

if (empty($_SESSION['userSession'])) {
    die();
}else{
    $user_id= $_SESSION['userSession'];

    $verification = $user_home-&gt;runQuery(
        "SELECT verification_status FROM verification WHERE user_id =:user_id");
    $verification-&gt;execute(array(":user_id"=&gt;$user_id));
    $verification_status = $verification-&gt;fetchAll(PDO::FETCH_ASSOC);

    echo json_encode(array("verification_status" =&gt; $verification_status));
    //sleep(rand(2,10));
}
?&gt;
</code></pre>