How to make Transfer-Encoding: chunked work?

2,432 views
Skip to first unread message

Eric Fong

unread,
Jul 18, 2010, 10:46:02 AM7/18/10
to nodejs
Hi

I have a stupid question. I have following code and hoping the data
chunks render one by one.
But my chrome wait 3 second and render everything all in one. Anyway
to make it works?

var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Transfer-Encoding': 'chunked'
});
setTimeout(function(){
response.write('25\n');
response.write('This is the data in the first chunk\n\n');
}, 1000);
setTimeout(function(){
response.write('1C\n');
response.write('and this is the second one\n\n');
}, 2000);
setTimeout(function(){
response.end('0');
}, 3000);
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');

Thanks

Terry Riegel

unread,
Jul 18, 2010, 12:45:36 PM7/18/10
to nod...@googlegroups.com
What if instead of using chinned encoding you just sent discrete HTML elements, like...

<div>line1</div>
wait 1 second
<div>line 2</div>
etc...

I doubt if you be able to change the behavior of Chrome.

Terry

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>

Matthew

unread,
Jul 18, 2010, 4:00:04 PM7/18/10
to nodejs
What does your client-side JS look like? If you're using the
onreadystatechange event to update the DOM then that's the problem.

From the 2006 W3C XHR draft (removed from the latest draft for some
reason):

onreadystatechange... represents a function that must be invoked when
readyState changes value. The function MAY be invoked multiple times
when readyState is 3 (Receiving). Its initial value must be null.

That MAY is crucial - it means that to adhere to spec, a browser
doesn't have to fire the onreadystatechange event multiple times for
the same readystate. So while some browsers will fire the event each
time a new chunk is received, others will fire it for the first chunk
and then only again when the data transfer has completed, when
readystate is 4.

I think that the only browser that fires the onreadystatechange event
multiple times for the same readystate (3) is Firefox.

Joe Shaw

unread,
Jul 18, 2010, 4:02:58 PM7/18/10
to nodejs
Hi,

On Jul 18, 10:46 am, Eric Fong <eri...@gmail.com> wrote:
> I have a stupid question. I have following code and hoping the data
> chunks render one by one.

I'm not sure it matters, but your chunks don't conform to the
requirements of the chunked TE in RFC 2616. Namely, the chunk size
and chunks are supposed to be terminated with a CRLF (\r\n). More
details here:

http://en.wikipedia.org/wiki/Chunked_transfer_encoding#Format

>   setTimeout(function(){
>           response.write('25\n');

This should be terminated with a CRLF (\r\n)

>           response.write('This is the data in the first chunk\n\n');

This too.

>   }, 1000);
>   setTimeout(function(){
>           response.write('1C\n');

Ditto.

>           response.write('and this is the second one\n\n');

Are you noticing a pattern? :)

>   }, 2000);
>   setTimeout(function(){
>           response.end('0');

Ditto. I suspect this is probably the most important one that you're
missing.

>   }, 3000);}).listen(8124);
>
> console.log('Server running athttp://127.0.0.1:8124/');

Hope this helps,

Joe

Mikeal Rogers

unread,
Jul 18, 2010, 5:22:45 PM7/18/10
to nod...@googlegroups.com
node already does chunked encoding for you.

If you don't return a content-length in the headers you sent your writes will be chunked by default. You shouldn't need to worry about any of this.

-Mikeal

Cagdas Tulek

unread,
Jul 18, 2010, 10:18:16 PM7/18/10
to nod...@googlegroups.com
Does node also support loading a file from the middle, then?

Mikeal Rogers

unread,
Jul 18, 2010, 10:58:21 PM7/18/10
to nod...@googlegroups.com
I don't know what you mean? Can you explain?

Eric Fong

unread,
Jul 19, 2010, 2:23:32 AM7/19/10
to nodejs
Hi

node is already chunked for me ?
But my browser render the code all in once?
Is the chunked and progressive loading only work in Ajax ?

Eric
> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .

Mikeal Rogers

unread,
Jul 19, 2010, 2:32:03 AM7/19/10
to nod...@googlegroups.com
The browser will render chunked and fixed length iteratively as it comes off the socket. Chunked encoding won't effect that.

Also, the chunked encoding overhead is parsed out in ajax so you'll never see it directly.

-Mikeal

To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.

Cagdas Tulek

unread,
Jul 19, 2010, 3:03:44 AM7/19/10
to nod...@googlegroups.com
In HTTP/1.1, the browser can also ask for a specific part of a file by "Content-Range". (See 14.16 Content-Range)

Mikeal Rogers

unread,
Jul 19, 2010, 3:09:25 AM7/19/10
to nod...@googlegroups.com
But, not all servers support this.

mscdex

unread,
Jul 19, 2010, 3:31:27 AM7/19/10
to nodejs
On Jul 18, 10:18 pm, Cagdas Tulek <ctu...@gmail.com> wrote:
> Does node also support loading a file from the middle, then?

You can read a file at any position you want. Take a look at fs.read()
here: http://nodejs.org/api.html#fs-read-141

Cagdas Tulek

unread,
Jul 19, 2010, 3:54:48 AM7/19/10
to nod...@googlegroups.com
Sure, I was just curious.

Timothy Caswell

unread,
Jul 19, 2010, 10:51:51 AM7/19/10
to nod...@googlegroups.com
I would love to implement this as part of the staticProvider layer in Connect. 

Alexey Petrushin

unread,
Jun 10, 2014, 7:13:09 AM6/10/14
to nod...@googlegroups.com
Does anyone managed to get it work in Chrome?

It works with curl, but it doesn't work with Chrome.
Reply all
Reply to author
Forward
0 new messages