0

The most relavent post I saw was: Streaming Http responses with NodeJS

Just to verify with others here, there is no longer any way from a browser to access the xhr.responseText before it is completed, right?

If impossible, I will just have to make all clients poll like 20 times a second or something.

Node.js server part

app.get('/peek', function(request, response) {
    console.log('   Client Connected');
    response.write('o');
    response.write('hai');
    response.write('der');
    response.end();
    setTimeout(function() { // Delay a bit
        response.writeContinue();
        response.write(' bear');
        response.end();
    }, 250);
}

Client JavaScript part

oReq.onreadystatechange=function() {
    switch(this.readyState) {
        case 1:
            ptr=this;
            console.log('Opened: ');
            break;
        case 2:
            console.log('Header Recieved: '+this.status);
            break;
        case 3:
            console.log('Loading: '+this.status);
            break;
        case 4:
            console.log('Recieved: '+this.status);
            break;
    }
    if(2<this.readyState) console.log(this.responseText);
}
Community
  • 1
  • 1
Nyaarium
  • 1,540
  • 5
  • 18
  • 34
  • There's an example at the beginning of https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest . It looks similar to what you are doing here. – Paul Aug 30 '13 at 01:59
  • 3
    Sounds like you need websockets. 20 times a second is just not feasible with xhr. – bfavaretto Aug 30 '13 at 02:12
  • If websockets aren't an option, long-polling might be. You can get very low latency that way (polling will likely be 1-2 seconds at best, and that will hit your servers pretty hard). If you give more information about your problem, we will be better able to help you. – Aaron Dufour Aug 30 '13 at 02:58
  • 1
    See http://stackoverflow.com/questions/6789703/how-to-write-javascript-in-client-side-to-receive-and-parse-chunked-response-i – bfavaretto Aug 30 '13 at 02:59
  • long-polling would be worse if you want split-second responses frequently. You want it realtime go with websockets. – user568109 Aug 30 '13 at 04:02
  • haha yeah, I didn't wanna resort to polling super fast. It is going to be a small game server. I am bout to do a hackathon with some friends. Getting some practice with Node.js before hand. – Nyaarium Aug 30 '13 at 04:54
  • @bfavaretto Since I am coding for a browser, there is no ability to access the data when streaming. I tried something like that already. – Nyaarium Aug 30 '13 at 04:56

0 Answers0