Comet or ajax Polling, which is better for me?

325 Views Asked by At

i am working on php website which involve large amount of data. i am trying to implement comet instead of simple ajax polling.

i am fetching data from database whenever someone insert data. i have 2 php pages :

long.php

<script type="text/javascript" >
var lastid = null;

$(document).ready(function(){
   lastid = $('.what').children().last().attr('id');
});

    var lastid = null;
    function waitForMsg(){
        lastid = $('.what').children().last().attr('id');
        $.ajax({
            type:"GET",
            url:"pollcheck.php?lastid="+lastid,
            async:true,
            cache:false,
            success: function(data){
                  $('.what').append(data);
                setTimeout('waitForMsg()', 1000);
            }   
    });
}

$(document).ready(function() {
    waitForMsg();   
});

</script>

and pollcheck.php :

<?php

include('connection.php');
include('config.php');

$lastmodif = isset($_GET['lastid']) ? $_GET['lastid'] : 0;

$queryfetchpollid = "select * from poll where id > $lastmodif";
$resultfetchpollid = mysqli_query($con, $queryfetchpollid);

$num_rows =  mysqli_num_rows($resultfetchpollid);

while($num_rows <= 0) {
    usleep(10000);
    clearstatcache();

$queryfetchpollid = "select * from poll where id > $lastmodif";
    $resultfetchpollid = mysqli_query($con, $queryfetchpollid);
    while($resultrow = mysqli_fetch_array($resultfetchpollid)){ $lastmodif = $resultrow[0]; }
    $num_rows =  mysqli_num_rows($resultfetchpollid);
}


while($resultrow = mysqli_fetch_array($resultfetchpollid)){
    $lastmodif = $resultrow[0];
    echo "<div class='rowt' id='$resultrow[0]'><div class='no'>$resultrow[0]</div><div class='name'>$resultrow[1]</div></div>";
}

?>

is this method better than simple ajax polling when server load is crucial ? do you know any other method ?

1

There are 1 best solutions below

0
Trapenok Victor On

Ajax polling make large number of query to you server. I recommend see this chat example

I think websockets will be best chose.

If you will be use example from this git repo your code will be similar to this:

pollcheck.php will be same as now.

In long.php will be this code:

$(document).ready(function() {

    // recive last messages from server when page opend
    $.ajax({
        type:"GET",
        url:"pollcheck.php?lastid=0",
        async:true,
        cache:false,
        success: function(data){
              $('.what').append(data);
        }   
    });

    // subscribe to new messages from websockets
    cometApi.start({node:"app.comet-server.ru", dev_id:15 }) 
    cometApi.subscription("simplechat.newMessage", function(event){
        $('.what').append(data);
    })

})

And when you made insert in to poll table in your database add this code after inserting into your database:

// host login and password from your installation
// of this  websockets server https://github.com/CppComet/comet-server
$host = "app.comet-server.ru";
$user = "15";
$password = "lPXBFPqNg3f661JcegBY0N0dPXqUBdHXqj2cHf04PZgLHxT6z55e20ozojvMRvB8";

// Connecting to CppComet api
$comet = mysqli_connect($host, $user, $password, "CometQL_v1");
if(mysqli_errno($comet))
{
    echo "Error:".mysqli_error($link);
}

// payload of your message
$msg = Array( "name" => $_POST["name"], "text"  => $_POST["text"] );
$msg = json_encode($msg);
$msg = mysqli_real_escape_string($comet, $msg);

// send message to webpage over websockets
$query = "INSERT INTO pipes_messages (name, event, message)" .
  "VALUES('simplechat', 'newMessage', '".$msg."')";

mysqli_query($comet, $query); 
if(mysqli_errno($comet))
{
    echo "Error:".mysqli_error($comet);
} 
else
{
    echo "ok";
}

In this example will be send message to your webpage with CppComet api