I have built a chat application using server sent events .But sse are not supported across all browsers.I was planning to use long polling ajax for polling source page for browser which do not support sse.
What the server page does is check db for new messages and output it using echo statement.
By refreshing page every X seconds using header("refresh: X;");
I saw another approach by which the
while(1){
echo statemenst....
echo statemenst....
sleep(5);
}
So my question is: If I sent ajax request with a very large waiting time will it receive response If header("refresh: X;"); is used .Or I need to use the infinite loop?.And if page refreshes will the requests to that page that are waiting for response get affected in someway ?
function waitForMsg(){
/* This requests the url "msgsrv.php"
When it complete (or errors)*/
$.ajax({
type: "GET",
url: "msgsrv.php",
async: true, /* If set to non-async, browser shows page as "Loading.."*/
cache: false,
timeout:50000, /* Timeout in ms */
success: function(data){ /* called when request to barge.php completes */
addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
setTimeout(
waitForMsg, /* Request next message */
1000 /* ..after 1 seconds */
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg, /* Try again after.. */
15000); /* milliseconds (15seconds) */
}
});
};