Search code examples
node.jssocketsstreamduplex

How to code two-way duplex streams in NodeJS


In the latest few versions of NodeJS (v0.10.X as of writing), the Streams API has undergone a welcome redesign and I would like to start using it now.

I want to wrap both the input and output of a socket with an object which implements a protocol.

The so-called Duplex interface, seems to just be any stream which is readable and writable (like a socket).

It is not clear whether Duplexes should be like A or B, or whether it doesn't matter.

   +---+        +---+
-->| A |-->     |   |-->
   +---+        | B |
                |   |<--
                +---+

What is the correct code-structure/interface for an object which has two writeables and two readables?

+--------+   +----------+   +----
|       r|-->|w        r|-->|w    
| socket |   | protocol |   | rest of app
|       w|<--|r        w|<--|r    
+--------+   +----------+   +----

The problem with the diagram above is that the protocol object needs two separate read methods and two write methods.

Off the top of my head, I could make the protocol produce 'left' and 'right' duplex objects, or 'in' and 'out' duplex objects (to slice it a different way).

Are either of these the preferred way, or is there a better solution?


Solution

  •           |      app      |
              +---------------+
                  ^       |
                  |       V      
               +-----+ +-----+
               |     | |     |
    +----------|     |-|     |-+
    | protocol | .up | |.down| |
    +----------|     |-|     |-+
               |     | |     |
               +-----+ +-----+
                  ^       |
                  |       V
              +---------------+
              |     socket    |
    

    My solution was to make a Protocol class, which created an Up Transform and a Down Transform.

    The Protocol constructor passes a reference (to itself) when constructing the Up and Down transforms. The _transform method in each of the up and down transforms can then call push on itself, on the other Transform, or both as required. Common state can be kept in the Protocol object.