- [Narrator] The stream interface
provides us with the technique to read and write data.
We can use it to read and write data to files,
to communicate with the internet,
to communicate with other processes.
In fact we've already been using it.
We've been using the stream interface
to read and write data to and from the terminal.
I'm inside of the exercise files under chapter five,
chapter five lesson one within the start folder
and we are looking at the read stream.js file.
This code should look familiar.
We are using process.standard input to listen
to data events on the terminal.
So let me go ahead and run this file by
typing node readStream
and we'll see what's happening as its asking
us to type something, so we will type a chunk of text
in the sense that I've typed five characters of text.
If I type hello world we have 11 characters of text.
Process.standard input is a readable stream.
We read the data by listening to data events.
The file system also comes with a way
to create readable streams.
I'm going to come in here to the top of this file
and include the fs module.
So I'll require fs
and I can create a readable stream with
the create readStream function.
The first argument that this function
wants is the path to the file that should be read.
So within our assets folder we'll notice
that there's a lorum-ipsum.md file,
this is a huge text file just filled
with lorum-ipsum text.
So we will add the path to that.
We can create binary read streams
or text read streams.
So if I wanted a text read stream
I would just pass the encoding UTF-8
as the second argument of the create
readStream function.
So now we've created a readable stream.
Instead of reading this file all at once,
we're going to be able to read it
bit by bit or chunk by chunk
and we read each of those chunks
by listening to data events.
So just like process standard input
is a readable stream that listens to data events,
I can go ahead and replace that with our readStream,
so now when I open up the terminals
we can go ahead and type node readStream
and you'll notice that we're using
the exact same function to read a text file.
So this text file is being read bit by bit
or chunk by chunk.
Reading files with streams causes
your nodejs application to use less memory
because instead of reading everything
all at once and loading it into a buffer,
you're reading files bit by bit and chunk by chunk.
They give us a lot of control because
they raise events.
So for instance we could also
say readStream.once
and wire up additional handlers to listen for data events.
So the very first bit of data,
we'll just go ahead and put that on the console,
go ahead and open up the terminal again
and run our readStream.
So you can see that every data
event is still being raised,
we're still reading all of the chunks of data
with the handler that we created on line six
but also we're reading the first little bit of data
so we can see here this is what the first little
bit of that lorum-ipsum text file looks like.
So that's powerful.
We can actually add a lot of events
to listen for our data as it's being read.
So streams also raise an end event
and we can use the end event to know that
the readStream has completed.
So we'll go ahead and open up the terminal now
and run our application again
and then we can see that once
the stream has finished
we have log to the console finished reading file.
So if we wanted to we could actually collect
all of these little bits of data in each data event.
So let's say I created a variable called file text
and we will set it to a string,
and then within this data event down here
I can just basically add up all my little data chunks.
So then as we read this data bit by bit
we're reading it into the file text variable.
So if I wanted to report on how long the entire file is
I can do so here in the end event.
And we can look at the total length of the file
by taking a look at the length of that file
text variable minus one.
So let's go ahead and open up the terminal
clear the current contents and node
readStream one last time.
And we see that file text is not defined
that's because I called it fileTXT
so lets go into line 19 and make that change there
and let's clear the console and try this one last time.
So in doing so we can see that we're reading all
of the text bit by bit but in the end
once we've gathered up all of those bits
we can see how many characters of text
that is in total.
As a notejs developer you will use the
stream interface constantly.
It's baked into everything that
reads and writes data.
We've already been using streams
and we will continue to use streams.