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 --> | ||
= | = Usage = | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="javascript"> | |||
// pipelines | |||
stream1 | |||
.pipe(stream2) | |||
.pipe(stream3) | |||
</syntaxhighlight> | |||
</blockquote><!-- --> | |||
= Stream Types = | |||
<blockquote> | |||
== Readable == | |||
<blockquote> | |||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
// readable-event, like select | |||
stream.on('readable', () => { console.log(stream.read()) }) | |||
// pipe readable to writable | |||
stream.pipe(writableStream) | |||
// write to readable stream | |||
stream.push('') | stream.push('hi') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- | </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 writeDuplex
Transform