i made a chat using php and ajax, and i am using a while loop to check the database for new messages.
this is the code that retrieves the message:
//retrive message
function update(){
$(document).ready(function(){
$.ajax({
async: true,
type: "POST",
url: "listen.php",
success: function(data){
$("#myp").before(data);
},
complete: function(){
window.setTimeout("update();",100);
}
});
});
};
//wating for new message
<?php
include_once("connect.php");
$type="";
while($type!=='n'){
usleep(1000);
$search=mysql_query("SELECT * from chat ORDER BY id DESC LIMIT 1");
$row=mysql_fetch_assoc($search);
$type=$row['type'];
$id=$row['id'];
}
echo $row['message'] . "<br/>";
mysql_query("UPDATE chat SET type='o' WHERE id=$id");
?>
now this works fine,the php file constantly checks to see if there are any new messages, the update function starts when the page is loaded and waits for the response. but is it efficient? if i would use this on a website, im afraid it would stress the server too much, because of the while loop. does anyone know of a way, to make that while loop more server friendly?