Answer the question
In order to leave comments, you need to log in
Comet(Long polling) - php + jquery?
Could you briefly explain, with examples, how a comet(long polling) application works with jquery and php? How specifically to create an ajax connection that lasts a certain amount of time (due to what)?
Answer the question
In order to leave comments, you need to log in
js creates a connection (in the options for ajax request a longer timeout is set)
php accepts it and see what can be sent. And waits... waits until there is data to send.
As soon as the data appears (let's say the script reads the data from the database or from the queue with some delay), then it sends this data and breaks the connection.
js script accepts data, sends data for processing and creates a new connection.... and so on ad infinitum.
In fact, instead of polling the server once a second, a connection is created that lives only until the data is received. After receiving the data, the connection is closed. After the connection is closed (it doesn’t matter if the data has arrived or an error / timeout has occurred), a new one is also created. In practice, the timeout is set to 20-30 seconds to avoid possible problems.
I can't immediately receive new data - they come only after a new execution of the getmess function, which is executed when the timer expires, what could be the error?
function getmess(){
$.ajax({
url:"get_mess.php",
type:"POST",
data:{"id":id},
cahce:false,
timeout:30000,
async:true,
success:function( result){
$("#response").html(result);
setTimeout('getmess()',10000);
}
});
}
on the server:
while(true){
$a=$_POST["id"];
$find_mess=(mysql_query("SELECT * FROM saymon WHERE id> '$a' "));
if (mysql_num_rows($find_mess)) {
while($row=mysql_fetch_array($find_mess)){
echo $row['mess']."";
}
flush();
exit;
}
sleep(5);
}
mysql_close();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question