﻿1
00:00:01,280 --> 00:00:04,030
‫Streams are yet another fundamental concept

2
00:00:04,030 --> 00:00:05,260
‫in Node.JS.

3
00:00:05,260 --> 00:00:07,063
‫So let's now learn all about them.

4
00:00:08,370 --> 00:00:11,020
‫So first of all, what are streams?

5
00:00:11,020 --> 00:00:15,530
‫Well, with streams we can process meaning read and write

6
00:00:15,530 --> 00:00:19,150
‫data piece by piece without completing the whole read

7
00:00:19,150 --> 00:00:20,760
‫or write operation.

8
00:00:20,760 --> 00:00:23,930
‫Therefore we don't have to keep all the data in memory

9
00:00:23,930 --> 00:00:26,080
‫to do these operations.

10
00:00:26,080 --> 00:00:29,020
‫For example, when we read a file using streams,

11
00:00:29,020 --> 00:00:31,990
‫we read part of the data, do something with it,

12
00:00:31,990 --> 00:00:36,080
‫then free our memory, and repeat this until the entire file

13
00:00:36,080 --> 00:00:37,510
‫has been processed.

14
00:00:37,510 --> 00:00:39,870
‫Or think of YouTube or Netflix,

15
00:00:39,870 --> 00:00:42,210
‫which are both called streaming companies

16
00:00:42,210 --> 00:00:45,540
‫because they stream video using the same principle.

17
00:00:45,540 --> 00:00:48,940
‫So instead of waiting until the entire video file loads,

18
00:00:48,940 --> 00:00:52,051
‫the processing is done piece by piece or in chunks

19
00:00:52,051 --> 00:00:55,700
‫so that you can start watching even before the entire file

20
00:00:55,700 --> 00:00:57,180
‫has been downloaded.

21
00:00:57,180 --> 00:01:00,140
‫So the principal here is not just about Node.JS.

22
00:01:00,140 --> 00:01:03,620
‫But universal to computer science in general.

23
00:01:03,620 --> 00:01:07,250
‫So as you can see, this makes streams the perfect candidate

24
00:01:07,250 --> 00:01:10,490
‫for handing large volumes of data like for example,

25
00:01:10,490 --> 00:01:14,280
‫video or also data that we're receiving piece by piece

26
00:01:14,280 --> 00:01:16,250
‫from an external source.

27
00:01:16,250 --> 00:01:19,270
‫Also, streaming make the data processing more efficient

28
00:01:19,270 --> 00:01:21,960
‫in terms of memory because there is no need

29
00:01:21,960 --> 00:01:25,530
‫to keep all the data in memory and also in terms of time

30
00:01:25,530 --> 00:01:28,470
‫because we can start processing the data as it arrives,

31
00:01:28,470 --> 00:01:31,223
‫rather than waiting until everything arrives.

32
00:01:32,430 --> 00:01:35,210
‫Okay, so now that we know what streams are,

33
00:01:35,210 --> 00:01:37,870
‫let's talk a little bit about how they are implemented

34
00:01:37,870 --> 00:01:39,100
‫in Node.JS.

35
00:01:39,100 --> 00:01:43,197
‫So in Node, there are four fundamental types of streams:

36
00:01:43,197 --> 00:01:46,583
‫readable streams, writable streams, duplex streams,

37
00:01:46,583 --> 00:01:48,700
‫and transform streams.

38
00:01:48,700 --> 00:01:50,780
‫But the readable and writeable ones

39
00:01:50,780 --> 00:01:52,370
‫are the most important ones.

40
00:01:52,370 --> 00:01:55,370
‫And so we're gonna focus more on these two.

41
00:01:55,370 --> 00:01:58,660
‫So readable streams are the ones from which we can read.

42
00:01:58,660 --> 00:02:02,410
‫We can consume data, makes sense right?

43
00:02:02,410 --> 00:02:06,080
‫Now streams are everywhere in the core Node modules.

44
00:02:06,080 --> 00:02:09,010
‫So a bit like events, like we talked about before.

45
00:02:09,010 --> 00:02:12,490
‫For example, the data that comes in when an http server

46
00:02:12,490 --> 00:02:15,700
‫gets a request is actually a readable stream.

47
00:02:15,700 --> 00:02:19,160
‫So all the data that is sent with the request comes in

48
00:02:19,160 --> 00:02:22,490
‫piece by piece and not in one large piece.

49
00:02:22,490 --> 00:02:25,270
‫Also another example from the file system

50
00:02:25,270 --> 00:02:27,867
‫is that we can read a file piece by piece

51
00:02:27,867 --> 00:02:31,170
‫by using a read screen from the FS module,

52
00:02:31,170 --> 00:02:35,010
‫which can actually be quite useful for large text files.

53
00:02:35,010 --> 00:02:37,400
‫All right, now another important thing to note

54
00:02:37,400 --> 00:02:39,994
‫is that streams are actually instances

55
00:02:39,994 --> 00:02:42,320
‫of the EventEmitter class.

56
00:02:42,320 --> 00:02:45,490
‫Meaning that all streams can emit and listen

57
00:02:45,490 --> 00:02:46,730
‫to named events.

58
00:02:46,730 --> 00:02:49,530
‫Just like we learned in the last lecture.

59
00:02:49,530 --> 00:02:52,260
‫In the case of readable streams, they can emit,

60
00:02:52,260 --> 00:02:54,710
‫and we can listen to many different events.

61
00:02:54,710 --> 00:02:58,920
‫But the most important two are the data and the end events.

62
00:02:58,920 --> 00:03:01,660
‫The data event is emitted when there is a new piece

63
00:03:01,660 --> 00:03:03,020
‫of data to consume,

64
00:03:03,020 --> 00:03:05,710
‫and the end event is emitted as soon as

65
00:03:05,710 --> 00:03:07,920
‫there is no more data to consume.

66
00:03:07,920 --> 00:03:09,570
‫And of course, we can then react

67
00:03:09,570 --> 00:03:11,220
‫to these events accordingly.

68
00:03:11,220 --> 00:03:14,670
‫And actually we're gonna do exactly that in the next video.

69
00:03:14,670 --> 00:03:17,193
‫So to practice how to work with streams.

70
00:03:18,170 --> 00:03:21,350
‫Finally, besides events, we also have important functions

71
00:03:21,350 --> 00:03:23,050
‫that we can use on streams.

72
00:03:23,050 --> 00:03:25,110
‫And in the case of readable streams,

73
00:03:25,110 --> 00:03:28,770
‫the most important ones are the pipe and the read functions.

74
00:03:28,770 --> 00:03:31,760
‫And again, you will see these in action in the next video,

75
00:03:31,760 --> 00:03:34,470
‫especially the super important pipe function,

76
00:03:34,470 --> 00:03:37,520
‫which basically allows us to plug streams together,

77
00:03:37,520 --> 00:03:39,960
‫passing data from one stream to another

78
00:03:39,960 --> 00:03:43,040
‫without having to worry much about events at all.

79
00:03:43,040 --> 00:03:45,893
‫Okay, next up, writeable streams are the ones

80
00:03:45,893 --> 00:03:48,060
‫to which we can write data.

81
00:03:48,060 --> 00:03:51,330
‫So basically, the opposite of readable streams.

82
00:03:51,330 --> 00:03:55,440
‫A great example is the http response that we can send back

83
00:03:55,440 --> 00:03:58,530
‫to the client and which is actually a writeable stream.

84
00:03:58,530 --> 00:04:01,760
‫So a stream that we can write data into.

85
00:04:01,760 --> 00:04:04,650
‫So when we want to send data, we have to write it somewhere,

86
00:04:04,650 --> 00:04:05,483
‫right?

87
00:04:05,483 --> 00:04:09,200
‫And that somewhere is a writeable stream,

88
00:04:09,200 --> 00:04:11,570
‫and that makes perfect sense, right?

89
00:04:11,570 --> 00:04:14,750
‫For example, if we wanted to send a big video file

90
00:04:14,750 --> 00:04:17,182
‫to a client, we would stream that result

91
00:04:17,182 --> 00:04:20,120
‫just like Netflix or YouTube do.

92
00:04:20,120 --> 00:04:23,130
‫Now about events, the most important ones are

93
00:04:23,130 --> 00:04:25,268
‫the drain and the finish events.

94
00:04:25,268 --> 00:04:27,040
‫And the most important functions are

95
00:04:27,040 --> 00:04:29,000
‫the write and end functions,

96
00:04:29,000 --> 00:04:31,850
‫some of which you will see in action in the next lecture.

97
00:04:32,720 --> 00:04:34,830
‫Now quickly about duplex streams.

98
00:04:34,830 --> 00:04:38,070
‫They're simply streams that are both readable and writeable

99
00:04:38,070 --> 00:04:39,530
‫at the same time.

100
00:04:39,530 --> 00:04:41,140
‫These are a bit less common.

101
00:04:41,140 --> 00:04:44,320
‫But anyway, a good example would be a web socket

102
00:04:44,320 --> 00:04:45,980
‫from the net module.

103
00:04:45,980 --> 00:04:49,194
‫And a web socket is basically just a communication channel

104
00:04:49,194 --> 00:04:53,130
‫between client and server that works in both directions

105
00:04:53,130 --> 00:04:56,840
‫and stays open once the connection has been established.

106
00:04:56,840 --> 00:05:00,191
‫Finally, transform streams are duplex streams,

107
00:05:00,191 --> 00:05:03,670
‫so streams that are both readable and writeable,

108
00:05:03,670 --> 00:05:06,588
‫which at the same time can modify or transform

109
00:05:06,588 --> 00:05:09,900
‫the data as it is read or written.

110
00:05:09,900 --> 00:05:12,970
‫A good example of this one is the zlib core module

111
00:05:12,970 --> 00:05:17,250
‫to compress data which actually uses a transform stream.

112
00:05:17,250 --> 00:05:20,220
‫All right, and that's the four types of streams

113
00:05:20,220 --> 00:05:23,300
‫and a broad overview of how we can use them.

114
00:05:23,300 --> 00:05:25,220
‫Now there is something important to mention here

115
00:05:25,220 --> 00:05:26,470
‫before we move on,

116
00:05:26,470 --> 00:05:29,140
‫which is a fact that these events and functions

117
00:05:29,140 --> 00:05:32,010
‫that I showed you are for consuming streams

118
00:05:32,010 --> 00:05:34,680
‫that are already implemented like the ones

119
00:05:34,680 --> 00:05:37,320
‫that I showed you here as our examples.

120
00:05:37,320 --> 00:05:41,000
‫So for example, Node implemented these http requests

121
00:05:41,000 --> 00:05:42,750
‫and responses as streams,

122
00:05:42,750 --> 00:05:45,550
‫and we can then consume, we can use them

123
00:05:45,550 --> 00:05:48,130
‫using the events and functions that are available

124
00:05:48,130 --> 00:05:50,170
‫for each type of stream.

125
00:05:50,170 --> 00:05:52,980
‫We could of course also implement our own streams

126
00:05:52,980 --> 00:05:56,660
‫and then consume them using these same events and functions.

127
00:05:56,660 --> 00:05:59,810
‫That however would be a video for another time

128
00:05:59,810 --> 00:06:01,810
‫because in order to build most apps,

129
00:06:01,810 --> 00:06:04,960
‫it's most important to know how to actually consume streams,

130
00:06:04,960 --> 00:06:06,980
‫not really how to implement them.

131
00:06:06,980 --> 00:06:09,130
‫All right, so let's now move on

132
00:06:09,130 --> 00:06:11,113
‫and actually use streams in practice.

