Wie kann ich nach Live-Updates suchen, ohne setInterval / timeout zu verwenden?

Beim Aufbau eines sozialen Netzwerks versuche ich, Live-Benachrichtigungen abzurufen. Derzeit sendet die Site mit setInterval alle paar Sekunden eine AJAX-Anforderung. Es sieht ungefähr so ​​aus:

setInterval ( function(){
    url = base_dir+"/ajax/file.php";
    data = "data=someData";
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        beforeSend: function(x) {
            if(x && x.overrideMimeType) {
                x.overrideMimeType("application/json;charset=UTF-8");
            }
        },
        success: function(JSON){
            // retrieve data here   
        }
    });
}, 5000);

Das funktioniert einwandfrei, aber ich mache mir große Sorgen darüber, dass die Server überlastet werden. Ich habe die Kometentechnik ausprobiert, aber aus irgendeinem Grund sendet sie viel mehr Anfragen als der obige Code. Gibt es eine andere nützlichere Technik, um diese Daten live zu übertragen?

EDIT: Für die Implementierung von Long Polling habe ich folgendes verwendet (hier genanntes Beispiel:http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery):

(function poll(){
    url = base_dir+"/ajax/file.php";
    data = "data=someData";
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType: "json",
        beforeSend: function(x) {
            if(x && x.overrideMimeType) {
                x.overrideMimeType("application/json;charset=UTF-8");
            }
        },
        success: function(JSON){
            // retrieve data here   
        },
complete: poll,
timeout: 5000
    });
})();

Es besteht die Möglichkeit, dass ich das Kometenprinzip nicht richtig verstehe.

PHP Code:

// Checks for new notifications, and updates the title and notifications bar if there are any
 private static function NotificationsCounter (){
    //self::$it_user_id                                     = query that retrieves my id for further checks;                                                        
    //$friend_requests_count                                = query that retrieves the friend requests count;
    //$updates_count                                        = query that retrieves the updates count;               
    $total_notifications                                    = $friend_requests_count+$updates_count;

    if ($total_notifications > 0) $addToTitle = "(".$total_notifications.")";
    else $addToTitle = "";

    if ($updates_count > 0) $counterHTML = "<span class='notification_counter' id='updates_counter' style='float: right;'>".$updates_count."</span>";
    else $counterHTML = "";

    $data = array("counter"=>$total_notifications,"addToTitle"=>$addToTitle,"counterHTML"=>$counterHTML,);
    echo json_encode($data); // parse to json and print
}

Da Facebook auch PHP verwendet, wie machen sie das?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage