Streaming across Node processes
Streams are about facilitating efficient, low memory data transfer, and processing; not just to and from the filesystem, but also to other processes and across network sockets.
In this recipe, we're going to write some simple command-line stream apps, then mix and match them with the common stream processing apps written in other languages.
Getting ready
Let's create two files: text_stream.js
and uppercaser.js
. Both of these need to be executable files as we're going to run them directly as command-line apps:
touch text_stream.js && chmod +x text_stream.js touch uppercaser.js && chmod + x uppercaser.js
How to do it…
Let's start by making a readable stream that randomly pushes lowercase alphabetical letters to its reader and pipes the stream to process.stdout
. In text_stream.js
, we will write:
#!/usr/bin/env node var stream = require('stream'); var util = require('util'); var textStream; function TextStream() { stream.Readable.call(this...