How it works...

The from2 module wraps the stream.Readable base constructor and creates the stream for us. It also adds some extra benefits, such as a destroy function to cleanly free up stream resources (across all Node versions) and the ability to perform asynchronous pushing (see the There's more... section for more).

Object mode
Like through2, both the from2 and to2 modules have obj methods which allow for convenient creation of object streams. See the There's more... section of the Creating transform streams recipe for more.

The to2 module is actually an alias for the flush-write-stream module, which similarly supplies a destroy function (for all Node versions), and the ability to supply a function (the flush function), which supplies final writes to the stream before it finishes.

When we pipe the rs stream to the ws stream, the Hello World string pushed (with rs.push) inside the read function passed to from2 is emitted as a data event, which the pipe method has hooked into so that the event causes a write to our ws stream. The write function (as supplied to the to call), dutifully logs out the Data written: Hello World message, and then calls cb to indicate it's ready for the next piece of data. The null primitive is supplied to the second call to rs.push inside the function supplied to the from invocation. This indicates that the stream has finished, and it triggers its own end event. Internally, an end event listener calls the end method on the destination stream (the stream passed to pipe, in our case ws).

At this point, our process has nothing left to do, and the program finishes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.162.26