Duplex Streams

A Duplex stream is a stream that combines Readable and Writable functionality. A good example of a Duplex stream is a TCP socket connection. You can read and write from the socket connection after it has been created.

To implement your own custom Duplex stream object, you need to first inherit the functionality for Duplex streams. The simplest way to do this is to use the following code, which uses the util module’s inherits() method:

var util = require('util');util.inherits(MyDuplexStream, stream.Duplex);

Then you create an instance of the object call:

stream.Duplex.call(this, opt);

The opt parameter with creation of a Duplex stream accepts an object with the property ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.