Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding streams in Node.js [closed]

Tags:

stream

node.js

I am currently trying to wrap my head around Node's streams. I think I got the idea, but I am not sure. What puzzles me is this quote by Dominic Tarr:

Streams are nodes best and most misunderstood idea, [...]

As Dominic is definitely a very clever guy (and I guess more clever than me) I am not sure if I really understood streams correctly, or if I missed the point ;-). Hence I would like to explain how I understood streams and ask you for approval or refusal.

My basic understanding of streams is that streams are an abstract wrapper of transporting data from a source to a target. There are readable and writable streams, and duplex stream which are readable and writeable. The idea is to abstract away the actual source or target, so that you can focus on dealing with streams without the need to know what actual resource you are dealing with. You are just accessing a stream for reading and / or writing.

Is my understanding of Node.js streams correct? Basically, this is the same idea as it is with streams in .NET (which has been my background for the last 12 years or so). How do Node.js streams differ from streams in .NET?

To give an example: It does not matter if I want to read from a file or a network resource, I just abstract the actual source away using a stream and then read from that stream. I could even change the source, but my stream-reading code would still work exactly the same way. Is this correct?

like image 430
Golo Roden Avatar asked Jan 27 '13 18:01

Golo Roden


1 Answers

I think you got it right. Streams in Node work just like garden hoses. You can connect a source and a destination (either as readable/writable or duplex stream), you can also connect the output of a stream to the input of another one, further extending the way the data goes.

Substack gave a great talk about Streams in Node.js at last year's LXJS conference:
LXJS 2012 - James Halliday - Harnessing The Awesome Power Of Streams

It is very compressed (you have to pause the video from time to time, because he's live-coding incredibly fast) but gives a good impression on how streams work.

like image 170
Frederic Avatar answered Oct 22 '22 17:10

Frederic