Streams in node js

Preview:

Citation preview

Getting Started With

STREAMS IN NODE JS

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

Streams Vs Full load

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

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

Understanding Streams

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

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

Streams: Event Flow

Event : START / READABLE

Source!Readable Stream

Sink!Writable Stream

START COMMUNICATION

Streams: Event Flow

Event : DATA

Source!Readable Stream

Sink!Writable Stream

SENDING DATA

Streams: Event Flow

Event : DATA

Source!Readable Stream

Sink!Writable Stream

SENDING DATA

Streams: Event Flow

Event : END

Source!Readable Stream

Sink!Writable Stream

END COMMUNICATION

Streams: Event Flow

Event : CLOSE

Source!Readable Stream

Sink!Writable Stream

CLOSE COMMUNICATION

Understanding Streams

PROGRAMATICALLY Lets see how we can use them in NodeJS

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.

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()

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.

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

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

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.

What is?

DUPLEX STREAM Combination of both readable and writable stream.

What is?

TRANSFORM STREAM

Same as Duplex, though their output is derived from input. They transform input to a different form and size output.

What next?

More Complex Examples

Lets see them!

Complex Examples

❖ STREAMS……

Example!&!

Demo Time

–Thank You

“presentation.pipe(audience)”