...

View Full Version : how to read streaming data in javascript



tejasagawane
09-12-2011, 11:45 AM
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?

Kor
09-12-2011, 01: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)

http://en.wikipedia.org/wiki/Push_technology
http://today.java.net/article/2010/03/31/html5-server-push-technologies-part-1
http://stackoverflow.com/questions/1253683/websocket-for-html5

rnd me
09-12-2011, 04: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...

niralsoni
09-12-2011, 05: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
*/
callBackFunction(response);
}
}

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum