Streams in node js
-
Upload
kushal-likhi -
Category
Documents
-
view
70 -
download
5
Transcript of Streams in node js
![Page 1: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/1.jpg)
Getting Started With
STREAMS IN NODE JS
By:!Kushal Likhi!!Daytime: !Innox Technologies
![Page 2: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/2.jpg)
Streams Vs Full load
WHY? Why do we need streams and why they are great?
![Page 3: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/3.jpg)
Why Streams?
"We should have some ways of connecting programs like garden
hose--screw in another segment when it becomes necessary to
massage data in another way. This is the way of IO also.”!
- Doug McIlroy
❖ Process large amounts of data with least memory footprint.!
❖ Manage Throttle - Producer consumer.!
❖ Separation of concerns, enforces building reusable components with defined behaviour.
3
![Page 4: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/4.jpg)
Understanding Streams
CONCEPTUAL Let’s understand conceptually what streams are in programming world.
![Page 5: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/5.jpg)
The General Idea { readableStream.pipe(writableStream) }
What are Streams? Here we let the data flow in small units from source to destination.
Glacier!Source Of Data!
Readable Stream
Ocean!Target For Data!
Writeable Stream
Pipe!Connection between the streams
![Page 6: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/6.jpg)
Streams: Event Flow
Event : START / READABLE
Source!Readable Stream
Sink!Writable Stream
START COMMUNICATION
![Page 7: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/7.jpg)
Streams: Event Flow
Event : DATA
Source!Readable Stream
Sink!Writable Stream
SENDING DATA
![Page 8: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/8.jpg)
Streams: Event Flow
Event : DATA
Source!Readable Stream
Sink!Writable Stream
SENDING DATA
![Page 9: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/9.jpg)
Streams: Event Flow
Event : END
Source!Readable Stream
Sink!Writable Stream
END COMMUNICATION
![Page 10: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/10.jpg)
Streams: Event Flow
Event : CLOSE
Source!Readable Stream
Sink!Writable Stream
CLOSE COMMUNICATION
![Page 11: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/11.jpg)
Understanding Streams
PROGRAMATICALLY Lets see how we can use them in NodeJS
![Page 12: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/12.jpg)
Some Basics
EVENT EMITTERS
Event Emitters are objects with following methods:!❖ .on(eventName, handler)!❖ .emit(eventName, [optional data])!And Following behaviour:!❖ On call to “on” method it will register a
handler for the specified event.!❖ On call to “emit” method it will call all
registered handlers for specified event.
![Page 13: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/13.jpg)
Streams Implementation❖ Streams are EventEmitters. Or in concrete words
Classes inheriting EventEmitter.!❖ They emit specific events:!
❖ start!❖ data!❖ end, finish!❖ error!❖ pipe, unpipe, drain!
❖ They have specific methods (Common ones):!❖ .pause()!❖ .pipe()!❖ .resume()!❖ .read()!❖ .write()
![Page 14: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/14.jpg)
Readable Stream
❖ Readable streams produce data that can be fed into a writable, transform, or duplex stream by calling .pipe()!
❖ readableStream.pipe(dest);!
❖ Readable streams will emit data events each time they get a "chunk" of data and then they will emit end when they are all finished.
![Page 15: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/15.jpg)
Readable Stream Example
❖ Lets make a program which can print the input strings in new lines and is pipe able in shell.
Example!&!
Demo Time
![Page 16: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/16.jpg)
Writable Stream❖ A writable stream is a stream you
can .pipe() to but not from:!❖ src.pipe(writableStream)!
❖ Examples:!❖ http requests, on the client!
❖ http responses, on the server!
❖ fs write streams!
❖ crypto streams!
❖ tcp sockets!
❖ child process stdin!
❖ process.stdout, process.stderr
![Page 17: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/17.jpg)
Writable stream Implementation❖ Writable streams must implement two functions: !
❖ write!
❖ end !
❖ When you write data to a writable stream it will return either true or false. !
❖ true means cool, keep sending more data with write.!
❖ false means Uh-oh I am backed up -- don't write any more data until I emit drain.
![Page 18: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/18.jpg)
What is?
DUPLEX STREAM Combination of both readable and writable stream.
![Page 19: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/19.jpg)
What is?
TRANSFORM STREAM
Same as Duplex, though their output is derived from input. They transform input to a different form and size output.
![Page 20: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/20.jpg)
What next?
More Complex Examples
Lets see them!
![Page 21: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/21.jpg)
Complex Examples
❖ STREAMS……
Example!&!
Demo Time
![Page 22: Streams in node js](https://reader034.fdocuments.us/reader034/viewer/2022042511/55cf06f4bb61eb8f3c8b468e/html5/thumbnails/22.jpg)
–Thank You
“presentation.pipe(audience)”