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 ?
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:
And when you made insert in to
polltable in your database add this code after inserting into your database:In this example will be send message to your webpage with CppComet api