1
00:00:00,210 --> 00:00:05,100
All right, and once we know the theory behind the screams now, let's take a look at the practical

2
00:00:05,100 --> 00:00:13,530
example and a very good use case is using streams wherein we are reading files because we need to understand

3
00:00:13,530 --> 00:00:20,330
one thing where when we use the sink or asynchronous approach, what happens?

4
00:00:20,340 --> 00:00:21,780
We're reading the whole file.

5
00:00:22,050 --> 00:00:26,550
And of course, in our example, we were setting this equal to a variable.

6
00:00:26,670 --> 00:00:27,150
Correct.

7
00:00:27,480 --> 00:00:33,500
But if we have a big file, well, first of all, you're just using all that memory.

8
00:00:33,900 --> 00:00:40,740
And second, as the file size gets bigger and bigger, bigger, eventually, the variable is not going

9
00:00:40,740 --> 00:00:41,430
to be good enough.

10
00:00:41,640 --> 00:00:49,240
You will get an error that I mean, the size is too big and you cannot place everything in the string.

11
00:00:49,470 --> 00:00:51,840
So what would be a solution?

12
00:00:52,140 --> 00:00:55,200
One solution would be read stream option.

13
00:00:55,650 --> 00:00:58,050
And the way will set this up.

14
00:00:58,440 --> 00:01:01,290
I'm going to start from scratch.

15
00:01:01,680 --> 00:01:08,420
But if you take a look at the report, you'll notice this 15 create big file.

16
00:01:08,730 --> 00:01:13,830
So previously when we were working with files, I just had some small files.

17
00:01:13,840 --> 00:01:14,050
Right.

18
00:01:14,070 --> 00:01:18,120
So I had the first sixty, the result and whatever.

19
00:01:18,420 --> 00:01:23,110
Now, before we set up the read stream, I would want to create a big file.

20
00:01:23,340 --> 00:01:25,170
Again, this is optional.

21
00:01:25,180 --> 00:01:26,490
You don't have to do that.

22
00:01:26,730 --> 00:01:32,280
But in my case, in order to show you how the streams work, yes, I'll have to set up a big file.

23
00:01:32,640 --> 00:01:37,200
And first, all remote because of course, I already have it since I was testing it.

24
00:01:38,040 --> 00:01:45,630
And now stop my server and of course, in my case, around 15, the create big file charges if you want

25
00:01:45,630 --> 00:01:51,600
to set it on your own and if you don't want to use the repo, you can just get the right file sync from

26
00:01:51,600 --> 00:01:52,470
the first module.

27
00:01:52,650 --> 00:02:01,680
And the notice I have the loop where I believe I have here ten thousand and every iteration.

28
00:02:01,890 --> 00:02:04,230
I just write a book the next day.

29
00:02:04,410 --> 00:02:11,000
I of course I have the flag up and I'm I'm just adding the world starting a new line.

30
00:02:11,280 --> 00:02:18,600
So if I run node and then of course I'm going to go with my file line which is 15, then hyphen.

31
00:02:19,560 --> 00:02:23,770
Create big file and yes.

32
00:02:24,060 --> 00:02:32,760
OK, so then in the content, I should have this big file and once I have my big file, I'll go back

33
00:02:32,760 --> 00:02:39,680
to Objets and I'll use my NPM start, since, of course, I have known my place and mine.

34
00:02:39,700 --> 00:02:40,210
Let's go.

35
00:02:40,680 --> 00:02:48,350
Now, I'm not going to show you how the setup would look like with file sync or the asynchronous one.

36
00:02:48,390 --> 00:02:55,740
Again, hopefully understand that setup would be exactly the same like we have in 10 and 11 where we

37
00:02:55,740 --> 00:02:58,800
have the FAA sync and FAA everything.

38
00:02:59,160 --> 00:03:02,150
And now, of course, let's just go with the stream option.

39
00:03:02,520 --> 00:03:11,460
So the method name is Create Read Stream and we require that from the first module like so.

40
00:03:12,370 --> 00:03:20,520
And then we create a new variable, and in my case, I'll call this stream and we invoke the create

41
00:03:20,710 --> 00:03:25,510
read stream and the only thing by default we need to pass in is the path.

42
00:03:25,690 --> 00:03:28,410
So, of course, in my case, I'm going to go with content and then force.

43
00:03:28,870 --> 00:03:35,950
And since I just created that text file, the big one, of course, I'm going to go big and I remember

44
00:03:35,950 --> 00:03:37,600
the good old friends events.

45
00:03:37,870 --> 00:03:42,550
Well, once we create this instance, we actually have access to them.

46
00:03:42,880 --> 00:03:47,770
And the ones that we're going to use is going to be data and error.

47
00:03:48,010 --> 00:03:53,950
So we can go with a stream and then aren't going to remember the events and text.

48
00:03:54,250 --> 00:03:58,690
And the event that you would want to listen for is a data.

49
00:03:58,900 --> 00:04:00,910
And then we have our callback function.

50
00:04:01,240 --> 00:04:04,800
And again, it is a parameter so we can call whatever we want.

51
00:04:04,810 --> 00:04:10,220
But in my case, I'm going to go result and let's just cancel the result.

52
00:04:10,420 --> 00:04:12,970
OK, so and what you'll notice.

53
00:04:14,080 --> 00:04:17,920
Is that we have 64 kilobytes here.

54
00:04:18,130 --> 00:04:19,380
That's our first console log.

55
00:04:19,730 --> 00:04:23,310
Then we have another one for 64 kilobytes.

56
00:04:23,380 --> 00:04:27,780
And the reason why I know that is because by default, that's the size we're getting.

57
00:04:28,030 --> 00:04:30,130
And then we are getting the reminder.

58
00:04:30,430 --> 00:04:37,570
Now, just to prove my point, I'm going to go to my folder and I'm going to look for my content.

59
00:04:38,200 --> 00:04:45,040
And then in there, if you take a look at the big text file we created is one hundred and sixty nine

60
00:04:45,040 --> 00:04:45,600
kilobytes.

61
00:04:45,970 --> 00:04:54,120
So as you can see now, we're reading data in chunks and by default that chunk is sixty four gigabytes.

62
00:04:54,670 --> 00:05:03,280
And every time we consume a log we see that we have 64, 64, and eventually we have the remainder.

63
00:05:03,580 --> 00:05:09,730
So instead of reading everything and placing that in a variable, we're doing that in chunks.

64
00:05:09,950 --> 00:05:14,980
And as you can see, we're using the event and the event is there.

65
00:05:15,310 --> 00:05:18,760
And in order to hammer this home, why don't we take a look at the docs?

66
00:05:19,240 --> 00:05:21,580
So first, I'm going to look for my file system.

67
00:05:22,590 --> 00:05:31,170
And then let's look for our create read stream so we go here, as we can see, we have this class read

68
00:05:31,170 --> 00:05:37,380
stream and on this class we have these three once we have close, open and ready.

69
00:05:37,600 --> 00:05:42,100
And as a side note, we'll use this one a little bit later as well, the open one.

70
00:05:42,120 --> 00:05:43,650
So keep an eye on this one.

71
00:05:44,040 --> 00:05:51,540
And then as far as the read stream, well, as you can see, it is created when we go to create a read

72
00:05:51,540 --> 00:05:53,160
stream and invoke it.

73
00:05:53,160 --> 00:05:53,480
Right.

74
00:05:53,760 --> 00:05:56,490
So I got the create read stream.

75
00:05:56,830 --> 00:05:57,720
That's my function.

76
00:05:57,900 --> 00:06:00,780
I just passed in the path and I invoked it.

77
00:06:01,020 --> 00:06:05,520
And in turn that returns the read stream.

78
00:06:06,120 --> 00:06:06,540
Right.

79
00:06:07,120 --> 00:06:12,130
An actual read stream extends from stream that readable.

80
00:06:12,420 --> 00:06:14,340
So right away I have these events.

81
00:06:14,340 --> 00:06:15,780
I have close, open and ready.

82
00:06:15,780 --> 00:06:21,850
And like I said, we use this open a little bit later now since it is extending from the stream.

83
00:06:22,200 --> 00:06:23,160
Check it out.

84
00:06:23,470 --> 00:06:25,140
Now, of course, I have the data one.

85
00:06:25,440 --> 00:06:25,810
Right.

86
00:06:25,980 --> 00:06:27,570
So this is a chunk of data.

87
00:06:27,810 --> 00:06:33,370
And as you can see, every time we are getting that data, we can do something with it.

88
00:06:33,510 --> 00:06:35,160
Now, at the moment, we're just logging.

89
00:06:35,400 --> 00:06:42,840
It's very simple, but eventually, as you can already imagine, we can do way more useful things than

90
00:06:42,840 --> 00:06:43,130
that.

