Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Streams are yet another fundamental concept
in Node.JS.
So let's now learn all about them.
So first of all, what are streams?
Well, with streams we can process meaning read and write
data piece by piece without completing the whole read
or write operation.
Therefore we don't have to keep all the data in memory
to do these operations.
For example, when we read a file using streams,
we read part of the data, do something with it,
then free our memory, and repeat this until the entire file
has been processed.
Or think of YouTube or Netflix,
which are both called streaming companies
because they stream video using the same principle.
So instead of waiting until the entire video file loads,
the processing is done piece by piece or in chunks
so that you can start watching even before the entire file
has been downloaded.
So the principal here is not just about Node.JS.
But universal to computer science in general.
So as you can see, this makes streams the perfect candidate
for handing large volumes of data like for example,
video or also data that we're receiving piece by piece
from an external source.
Also, streaming make the data processing more efficient
in terms of memory because there is no need
to keep all the data in memory and also in terms of time
because we can start processing the data as it arrives,
rather than waiting until everything arrives.
Okay, so now that we know what streams are,
let's talk a little bit about how they are implemented
in Node.JS.
So in Node, there are four fundamental types of streams:
readable streams, writable streams, duplex streams,
and transform streams.
But the readable and writeable ones
are the most important ones.
And so we're gonna focus more on these two.
So readable streams are the ones from which we can read.
We can consume data, makes sense right?
Now streams are everywhere in the core Node modules.
So a bit like events, like we talked about before.
For example, the data that comes in when an http server
gets a request is actually a readable stream.
So all the data that is sent with the request comes in
piece by piece and not in one large piece.
Also another example from the file system
is that we can read a file piece by piece
by using a read screen from the FS module,
which can actually be quite useful for large text files.
All right, now another important thing to note
is that streams are actually instances
of the EventEmitter class.
Meaning that all streams can emit and listen
to named events.
Just like we learned in the last lecture.
In the case of readable streams, they can emit,
and we can listen to many different events.
But the most important two are the data and the end events.
The data event is emitted when there is a new piece
of data to consume,
and the end event is emitted as soon as
there is no more data to consume.
And of course, we can then react
to these events accordingly.
And actually we're gonna do exactly that in the next video.
So to practice how to work with streams.
Finally, besides events, we also have important functions
that we can use on streams.
And in the case of readable streams,
the most important ones are the pipe and the read functions.
And again, you will see these in action in the next video,
especially the super important pipe function,
which basically allows us to plug streams together,
passing data from one stream to another
without having to worry much about events at all.
Okay, next up, writeable streams are the ones
to which we can write data.
So basically, the opposite of readable streams.
A great example is the http response that we can send back
to the client and which is actually a writeable stream.
So a stream that we can write data into.
So when we want to send data, we have to write it somewhere,
right?
And that somewhere is a writeable stream,
and that makes perfect sense, right?
For example, if we wanted to send a big video file
to a client, we would stream that result
just like Netflix or YouTube do.
Now about events, the most important ones are
the drain and the finish events.
And the most important functions are
the write and end functions,
some of which you will see in action in the next lecture.
Now quickly about duplex streams.
They're simply streams that are both readable and writeable
at the same time.
These are a bit less common.
But anyway, a good example would be a web socket
from the net module.
And a web socket is basically just a communication channel
between client and server that works in both directions
and stays open once the connection has been established.
Finally, transform streams are duplex streams,
so streams that are both readable and writeable,
which at the same time can modify or transform
the data as it is read or written.
A good example of this one is the zlib core module
to compress data which actually uses a transform stream.
All right, and that's the four types of streams
and a broad overview of how we can use them.
Now there is something important to mention here
before we move on,
which is a fact that these events and functions
that I showed you are for consuming streams
that are already implemented like the ones
that I showed you here as our examples.
So for example, Node implemented these http requests
and responses as streams,
and we can then consume, we can use them
using the events and functions that are available
for each type of stream.
We could of course also implement our own streams
and then consume them using these same events and functions.
That however would be a video for another time
because in order to build most apps,
it's most important to know how to actually consume streams,
not really how to implement them.
All right, so let's now move on
and actually use streams in practice.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.