My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Matteo Collina

43 likes

·

11.7K reads

12 comments

Vineeth Kasula
Vineeth Kasula
Jul 12, 2024

Thanks, This is really helpful

3
·
Jason Jiang
Jason Jiang
Jul 18, 2024

Thanks for the great guide, and I was confused with the Writeable demo: Why the "wait drain" was logged after only one HELLO is logged to stdout, I assume the buffer was empty after the first HELLO logged, the 2nd HELLO should also be logged before the first "wait drain", not sure where did i misunderstood, is this because the stdout.write is async, after calling the "cb", the "hello" in the buffer will be removed?

1
·
·1 reply
Matteo Collina
Matteo Collina
Author
·Jul 25, 2024

Good spot! The buffer is flushed on first write. Because the cb will be called after an event loop turn (it's async!), we need to wait for drain. To counteract this effect there are cork() and uncork(). They are used to send out the http headers for example.

·
Priyankar Pal
Priyankar Pal
Jul 20, 2024

Well written article. How did you design the diagram?

1
·
·1 reply
Matteo Collina
Matteo Collina
Author
·Jul 25, 2024

We have an amazing designer at platformatic :).

·
Sarfraz Ashrafi
Sarfraz Ashrafi
Jul 20, 2024

thank you mate informative

·
Stasa Stanisic
Stasa Stanisic
Jul 20, 2024

Thank you for a great streams overview!

How would you write out the correct version of HTTP stream example with stream/promises pipeline, pass res as last parameter? And for fastify, reply.send?

·
·3 replies
Matteo Collina
Matteo Collina
Author
·Jul 25, 2024

use @fastify/static :). Don't stream files directly because there are infinite edge cases that will inevitably result in a security issues in your server.

1
·
Stasa Stanisic
Stasa Stanisic
Jul 25, 2024

Matteo Collina Thanks for the reply! Yes, understood for static files which can be directly served, but I was thinking more of the case when there is a pipeline - reading file then transforming and finally sending as a response...

·
Matteo Collina
Matteo Collina
Author
·Jul 25, 2024

Stasa Stanisic you can just call reply.send(stream) or return stream in your Fastify route. Using barebone HTTP, you should do pipeline(a, b, c, res, (err) => { ... }).

·
Esten Grove
Esten Grove
Aug 2, 2024

Really great article. Would love for an in depth article on async generators. Saw a library recently using them for streams & it seemed very concise. Would be interested to leer more.

·