22
Getting Started With STREAMS IN NODE JS By: Kushal Likhi Daytime: Innox Technologies

Streams in node js

Embed Size (px)

Citation preview

Page 1: Streams in node js

Getting Started With

STREAMS IN NODE JS

By:!Kushal Likhi!!Daytime: !Innox Technologies

Page 2: Streams in node js

Streams Vs Full load

WHY? Why do we need streams and why they are great?

Page 3: Streams in node js

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

Understanding Streams

CONCEPTUAL Let’s understand conceptually what streams are in programming world.

Page 5: Streams in node js

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

Streams: Event Flow

Event : START / READABLE

Source!Readable Stream

Sink!Writable Stream

START COMMUNICATION

Page 7: Streams in node js

Streams: Event Flow

Event : DATA

Source!Readable Stream

Sink!Writable Stream

SENDING DATA

Page 8: Streams in node js

Streams: Event Flow

Event : DATA

Source!Readable Stream

Sink!Writable Stream

SENDING DATA

Page 9: Streams in node js

Streams: Event Flow

Event : END

Source!Readable Stream

Sink!Writable Stream

END COMMUNICATION

Page 10: Streams in node js

Streams: Event Flow

Event : CLOSE

Source!Readable Stream

Sink!Writable Stream

CLOSE COMMUNICATION

Page 11: Streams in node js

Understanding Streams

PROGRAMATICALLY Lets see how we can use them in NodeJS

Page 12: Streams in node js

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

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

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

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

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

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

What is?

DUPLEX STREAM Combination of both readable and writable stream.

Page 19: Streams in node js

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

What next?

More Complex Examples

Lets see them!

Page 21: Streams in node js

Complex Examples

❖ STREAMS……

Example!&!

Demo Time

Page 22: Streams in node js

–Thank You

“presentation.pipe(audience)”