Nodejs streams: Difference between revisions

From wikinotes
(Created page with " = Documentation = <blockquote> {| class="wikitable" |- | official docs || https://nodejs.org/api/stream.html |- | official tutorial || https://nodejs.dev/learn/nodejs-streams...")
 
No edit summary
 
Line 1: Line 1:
The official docs are very good here, this is just quick overview.


= Documentation =
= Documentation =
Line 11: Line 12:
</blockquote><!-- Documentation -->
</blockquote><!-- Documentation -->


= Basics =
= Usage =
<blockquote>
<blockquote>
types of streams
<syntaxhighlight lang="javascript">
* Readable
// pipelines
* Writable
stream1
* Duplex
  .pipe(stream2)
* Transform
  .pipe(stream3)
</syntaxhighlight>
</blockquote><!--  -->


= Stream Types =
<blockquote>
== Readable ==
<blockquote>
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
const Stream = require('stream');
// readable-event, like select
stream.on('readable', () => { console.log(stream.read()) })
 
// pipe readable to writable
stream.pipe(writableStream)


const stream = new Stream.Duplex()
// write to readable stream
stream.push('')
stream.push('hi')
stream.write('abc')
stream.end()        // signal end of writing
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Basics -->
</blockquote><!-- Readable -->
 
== Writable ==
<blockquote>
<syntaxhighlight lang="javascript">
stream.write('abc')  // write
stream.end()        // nothing left to write
</syntaxhighlight>
</blockquote><!-- Writable -->
 
== Duplex ==
<blockquote>
 
</blockquote><!-- Duplex -->
 
== Transform ==
<blockquote>
 
</blockquote><!-- Transform -->
</blockquote><!-- Stream Types -->

Latest revision as of 03:10, 6 August 2021

The official docs are very good here, this is just quick overview.

Documentation

official docs https://nodejs.org/api/stream.html
official tutorial https://nodejs.dev/learn/nodejs-streams

Usage

// pipelines
stream1
  .pipe(stream2)
  .pipe(stream3)

Stream Types

Readable

// readable-event, like select
stream.on('readable', () => { console.log(stream.read()) })

// pipe readable to writable
stream.pipe(writableStream)

// write to readable stream
stream.push('hi')

Writable

stream.write('abc')  // write
stream.end()         // nothing left to write

Duplex

Transform