I\'m trying to make a theoretical web chat application with php and jquery, I\'ve read about long polling and http streaming, and I managed to apply most principles introduc
How do I check for the results during the Ajax connection is still active? I'm aware of jQuery's success function for ajax calls, but how do I check the data while the connection is still ongoing?
Actually, you can. I've provided a revised answer for the above but I don't know if it's still pending or has been ignored. Providing an update here so that the correct information is available.
If you keep the connection between the client and the server open it is possible to push updates through which are appended to the response. As each update comes in the XMLHttpRequest.onreadystatechange
event is fired and the value of the XMLHttpRequest.readyState
will be 3. This means that the XMLHttpRequest.responseText continues to grow.
You can see an example of this here: http://www.leggetter.co.uk/stackoverflow/7213549/
To see the JS code simply view source. The PHP code is:
> ' . $update_suffix;
echo($message);
flush();
$count = $count + 1;
sleep($sleep_time);
}
?>
In Gecko based browsers such as Firefox it's possible to completely replaces the responseText
by using multipart/x-mixed-replace
. I've not provided an example of this.
It doesn't look like it's possible to achieve the same sort of functionality using jQuery.ajax
. The success
callback does not fire whenever the onreadystatechange
event is fired. This is surprising since the documentation states:
No onreadystatechange mechanism is provided, however, since success, error, complete and statusCode cover all conceivable requirements.
So the documentation is potentially wrong unless I'm misinterpreting it?
You can see an example that tries to use jQuery here: http://www.leggetter.co.uk/stackoverflow/7213549/jquery.html
If you take a look at the network tab in either Firebug or Chrome Developer tools you'll see the file size of stream.php
growing but the success
callback still isn't fire.