View Full Version : how to read streaming data in javascript

09-12-2011, 12:45 PM
I wants to access server web page which data is continuously growing(streaming data), I want to buffered some of that data write it in responseText then again get next buffered data write it and so on (in AJAX).
Is it possible? if yes how to do that?

09-12-2011, 02:28 PM
Probably you may want to test the Push technology (to let the server send data instead of forcing a refresh from the client side)


rnd me
09-12-2011, 05:40 PM
sure, just don't close the http connection on the server and both ajax and <script src="xxx"/> loads will wait until the connection is closed before parsing or firing onreadystatechange with a complete status...

09-12-2011, 06:09 PM
below is the sample code that might give you an idea of implementing your requirements.
its just a thought, and not a working model.

var start_index = 0;
var buffer_size = 1000;
var end_of_file = false;
var server_url = 'your_server_url_goes_here';

function getDataFromServer_AJAX(server_url, start_index, buffer_size, callBackFunction) {
if(!end_of_file) {
// add your AJAX call to server here, with start_index and buffer_size passed as a parameter
// define your desired reponse format. I have used following format -
response = {'status': 0, 'data': 'No data found / end of file' }
response = {'status': 1, 'data': 'my data string extracted from the stream' }
// you can add more response status codes as per need

function callBackFunction(response) {
if(response != null && response.status == 1) { // required data has been found
var data = response.data;
// process your data
// .........
// .........
// at the end, update the indexes
start_index += data.length + 1;
else {
// no data found, end of file etc
end_of_file = true;
// continue streaming the data
getDataFromServer_AJAX(server_url, start_index, buffer_size, callBackFunction);

Hope it may help you out..

Thanks & Regards,
Niral Soni