Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: What's the difference between a Duplex stream and a Transform stream?

Tags:

stream

node.js

The Stream docs state that Duplex Streams "are streams that implement both the Readable and Writable interfaces" and Transform Streams "are Duplex streams where the output is in some way computed from the input." Unfortunately, the docs do not describe what Transform streams provide above and beyond Duplex streams.

Are there any differences between the two? When would you use one over the other?

like image 822
brainkim Avatar asked Aug 20 '13 12:08

brainkim


People also ask

Is a transform stream a duplex stream?

A transform stream is basically a duplex stream that can be used to modify or transform the data as it is written and read. An example of that is the zlib.

What is Nodejs duplex stream?

Just as we explained earlier, the Duplex stream is basically a mixture of the Readable and Writable streams. An example of a Duplex stream is a Socket, which provides two channels to send and receive data. Other examples of the Duplex streams are: TCP sockets. zlib streams.

What is a transform stream?

A transform stream consists of a pair of streams: a writable stream, known as its writable side, and a readable stream, known as its readable side. Writes to the writable side result in new data being made available for reading from the readable side.

What are duplex streams?

- A duplex stream is a stream that implements both a readable and a writable. These streams allow data to pass through. Readable streams will pipe data into a duplex stream, and the duplex stream can also write that data. So duplex stream represent the middle sections of pipelines.


2 Answers

A Duplex stream can be thought of a readable stream with a writable stream. Both are independent and each have separate internal buffer. The reads and writes events happen independently.

                             Duplex Stream
                          ------------------|
                    Read  <-----               External Source
            You           ------------------|   
                    Write ----->               External Sink
                          ------------------|
            You don't get what you write. It is sent to another source.

A Transform stream is a duplex where the read and write takes place in a causal way. The end points of the duplex stream are linked via some transform. Read requires write to have occurred.

                                 Transform Stream
                           --------------|--------------
            You     Write  ---->                   ---->  Read  You
                           --------------|--------------
            You write something, it is transformed, then you read something.
like image 88
user568109 Avatar answered Oct 19 '22 06:10

user568109


The difference is just syntactic sugar. Transform streams are full duplex streams but rather than implementing _write and _read methods, you are asked to implement just the _transform method. You can read more about streams on the excellent substack's streams guide or from Isaacs's readable-stream repo.

like image 10
danielepolencic Avatar answered Oct 19 '22 05:10

danielepolencic