Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "Node Streams Overview" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Will creates a diagram of a Node stream and explains how streams processes large-scale data in a more efficient method.

Preview
Close

Transcript from the "Node Streams Overview" Lesson

[00:00:00]
>> Will Sentance: We're now gonna start working on things in batches. I love this. I think this is absolutely beautiful. By the way, what are the batch sizes? We can control it, set it ourselves, but the default batch size is 64 kilobytes. They call it the high watermark. That any read flow of data.

[00:00:18]
I'm gonna show you by the way why didn't think they could, well we'll get the read streaming thing in a second. I don't consider them streams of data. They're batches of data. They're brought in chunks of 64 kilobytes. It's not any stream I've heard about, where you're like, well, we'll see this in a moment.

[00:00:32]
Well, actually, we'll see it right now. This is, to me, what a stream looks like, and I did once do this, and drew out an entire bucolic environment with a full stream. But I'm not gonna do that this time, because that was a real waste of time and a massive distraction last time.

[00:00:48]
Just a little bit of a stream. So here's what to me a stream looks like. There you go, see. Definitely not drawing the whole thing this time. Just a little bit. That's a volcano in the background, no okay. That was meant to be a tree, that was meant to be.

[00:01:05]
Look, look, artistry is not, I talked about this before but it's my favorite thing. I'll never forget when I was six years old, when a girl in my, a women in my, well, girl in my class said, I said, can you draw a cow. And she drew, and it was Emma.

[00:01:21]
I still remember her, Emma Try, she lived down the street, shout out, Emma. She lived down the street, she went to the same church as me. Emma, I sang in the choir. Anyway, moving on, so Emma could draw a cow and I tell you if any of you tried to draw a cow, it's something very real for me.

[00:01:39]
If you try to draw a cow, you cannot draw a cow. It will look like a horse, or a dog and it's very fascinating to me. So, anyway that was not much to that story except for she could draw a cow.
>> Will Sentance: See if I can draw a cow.

[00:01:56]
People pay a subscription for this, people actually pay a subscription. Okay, there we go, that's why, okay.
>> Class: No.
>> Will Sentance: [LAUGH] Who said no? Virginia, you're just like the Virginia in my family, constantly undermining,
>> Class: [LAUGH]
>> Will Sentance: No, no, no, she's correct. All right, so this is what a stream looks like to me is a constant flow.

[00:02:17]
Whereas actually, what a stream is in node, and honestly in computer science in general, is chunks of data. So we're gonna get them like, think of it instead as pails of water. Pail, you only ever say the word pail when you're talking about, okay, buckets of water. Buckets of water with 64 kilobytes of data in each of them.

[00:02:44]
I think that's really important because the idea of stream sounds so intimidating what, flows of data racing towards me. How am I gonna deal with them? I don't have to. Instead they come in as buckets and each time a bucket arrives, I'm gonna flag an event, nodes, I'm not gonna do, Node's gonna auto broadcast a message.

[00:03:01]
What is the message? Well can anyone spot, Zep can you spot what the message might be?
>> Zep: I think it's data.
>> Will Sentance: It's data. It's gonna broadcast message data and we're gonna have a function attached to it to auto run at that moment back in JavaScript, again and again and again.

[00:03:16]
Okay, so, I just think the word stream sounds so intimidating. It's like it's flowing data racing towards me. How can I possibly write code to handle that? I don't have to. Matt's like, no, that's fine. But to me, I thought was very, I think it's very important. It's not a constant flow that's unwieldy and out of control.

[00:03:32]
It's a batch of data that we get to set a function that will autorun when a batch completes, when a batch fully gets into Node and gets executed, okay? By the way, people, this is Node's pride and joy feature. Node is so proud. Andrew go ahead.
>> Andrew: Is the data pre-batched in 64 kilobyte pails before Node interacts or is Node like counting, counting, coming like a bouncer.

[00:03:56]
Boom 64, hey, we got data. Next one in.
>> Will Sentance: It's handled by Node. Yeah, this is Node's pride and joy featured, people. This is a feature of Node that people are like, wow. Because they can see the efficiency this gives you. This is super efficient for when you're handling large scale data and want to continue moving in a single threaded environment and not block any further code running.

[00:04:19]
People love, love, love this design pattern of Node, its pride and joy feature. Along with the auto running stuff, this is probably its most important feature. Okay, let's get using it now though because we really got caught up there. That picture, I just don't think it's a good move to spend the time on it.

[00:04:35]
And I don't know, I felt like it was a good decision at the time.
>> Speaker 5: So is this feature why Netflix uses Node?
>> Will Sentance: Actually, that's very interesting. Exactly how video is passed to a client is also can be done, yes in chunks, and you can certainly use Node for it.

[00:04:58]
But when it comes to video servers, people use very dedicated other specialized technology often to handle serving a video. And when it comes to video streaming, that is where your streaming messages video, between two users, like we're sending a video chat. We don't even use the TCP, which is the standard under the hood format of rules for passing data back and forwards between a browser and a server on which HTTP sits.

[00:05:27]
We don't even use this protocol called TCP at all. We use a totally different set of rules for how you pass data back and forwards called UDP, which is more suited. Let's just do this bit and I might tell you TCP versus UDP in a second. It's a good thing to know.

[00:05:40]
But let's just do this for now.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now