7

I am experimenting with various responses from a simple NodeJS HTTP server. The effect I am trying to achieve is faster visual rendering of a web page. Since the response is streamed to the browser with transfer-encoding: chunked (right?) I was thinking I could render the page layout first and the rest of the data after a delay.

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {
        'Content-Type': 'text/html'
        , 'Transfer-Encoding': 'chunked'
    });
    res.write('<html>\n');
    res.write('<body>\n');
    res.write('hello ');
    res.write('</body>\n');
    res.write('</html>\n');
    setTimeout(function () {
        res.end('world');
    },1500);
}).listen(3000, '127.0.0.1');

The thing is that it seems as if the response isn't sent until res.end('world') unless the already written data is long enough, so for instanceres.write(new Array(2000).join('1')) instead of thatres.write('hello'), would do the trick.

Is Node buffering my writes until the data is sizable enough to be sent? If that is the case, is the chunk size configurable?

Daniel
  • 135
  • 1
  • 1
  • 8

1 Answers1

3

It's possible that the browser is not rendering the data until the closing tags have been read. Try outputting plain text instead of html tags to test this.

Do you see any input coming into firebug / chrome inspector?

Related Question

http://nodejs.org/api/stream.html#stream_stream_write_string_encoding_fd :

Writes string with the given encoding to the stream. Returns true if the string has been flushed to the kernel buffer. Returns false to indicate that the kernel buffer is full, and the data will be sent out in the future.

So output the results of .write() methods. See if it returns a true or false.

Community
  • 1
  • 1
Straseus
  • 478
  • 1
  • 4
  • 14
  • The output of .write() is `true` both times. That related question you linked suggests that this is platform specific, any idea as to what that would mean? The chrome inspector only outputs the completed response time, same for both cases. The data is also rendered before the closing tags have been read, it is the same behavior when using only plain text. – Daniel Mar 17 '12 at 19:29
  • 1
    It's browser specific. Try it out in different browsers. Also, in the chrome inspector's network tab, you you should be able to see the data come in live if you reload the page and click on the first item under the network tab. Make sure you select the preview tab on the right panel - here's what i am talking about: http://imgur.com/8CJD1 – Straseus Mar 18 '12 at 05:26
  • Yes, it sure seems like it is the browser that buffers the response. I tried running the request with curl and that produced the expected behavior, (although curl seemed to buffer if response lacked line-endings `\n`) – Daniel Mar 18 '12 at 09:43
  • @Daniel Have you found any fix for that ? – silkAdmin Mar 20 '12 at 10:11
  • @silkAdmin Cant say that I have found a fix for the issue where the HTTP response is buffered (for some reason) if it is not sufficiently large. But it do seems as if it is not a problem with Node, which answer my question I guess. – Daniel Mar 21 '12 at 10:46
  • I found answer here: http://stackoverflow.com/questions/6233562/node-js-chunked-transfer-encoding – Ivan Lazarevic Jul 22 '13 at 18:55