2

This is an example in the book : "Professional Node.js". It should output a timestamps every second for 3s. But nothings appear before the res.end() is called : (the page load while the 3s, before all appears on the screen...)

require('http').createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    var left = 3;
    var interval = setInterval(function () {
        res.write(Date.now() + " ");

        left -= 1;
        if (left === 0) {
            clearInterval(interval);
            res.end();
        }
    }, 1000);
}).listen(4000);

Is it the same with you ?

[Edit:] I find this Simple Node.js server which responds in chunked transfer encoding and it doesn't work for me either... (all is display after the end() call ~5s)

BeauCielBleu
  • 379
  • 2
  • 11
  • I think this is the same problem discuessed [here][1]. [1]: http://stackoverflow.com/questions/6068820/node-js-problems-with-response-write – c089 Sep 27 '13 at 11:10
  • Yeah i saw, but not my solution :/, neither : http://stackoverflow.com/questions/5420162/chrome-dont-wait-until-all-data-is-received-node-js?rq=1 – BeauCielBleu Sep 27 '13 at 13:13

1 Answers1

0

Works fine for me. So it depends on how do you test this, because browser, curl and things like that will buffer things.

GET / HTTP/1.1

HTTP/1.1 200 OK
Content-Type: text/plain
Date: Fri, 27 Sep 2013 11:21:22 GMT
Connection: keep-alive
Transfer-Encoding: chunked

e
1380280883375 
e
1380280884378 
e
1380280885380 
0
alex
  • 11,935
  • 3
  • 30
  • 42
  • I used telnet and it work too. But not in the browser :s I don't know why... Can I deactivate the buffer ? – BeauCielBleu Sep 27 '13 at 13:12
  • Sure, Chromium project is just over a million lines of code, you just need to find the right one. If you need auto-updating stuff in the browser, use the right tools for it, for example websockets. – alex Sep 27 '13 at 13:23
  • Chunked encoding is useful when you don't know how much information you have to transfer before you start doing it, for example for streaming. And it works. Nobody guarantees that this partial information will be displayed when it delivered. – alex Sep 28 '13 at 09:38
  • Ok thanks. For those who have to do the same research : http://stackoverflow.com/questions/13557900/chunked-transfer-encoding-browser-behavior and http://stackoverflow.com/questions/237553/google-chrome-and-streaming-http-connections – BeauCielBleu Sep 28 '13 at 12:41