NodeJS Stream splitting
<p>I have an infinite data stream from a forked process. I want this stream to be processed by a module and sometimes I want to duplicate the data from this stream to be processed by a different module (e.g. monitoring a data stream but if anything interesting happens I want to log the next n bytes to file for further investigation).</p>

<p>So let's suppose the following scenario:</p>

<ol>
<li>I start the program and start consuming the readable stream</li>
<li>2 secs later I want to process the same data for 1 sec by a different stream reader</li>
<li>Once the time is up I want to close the second consumer but the original consumer must stay untouched.</li>
</ol>

<p>Here is a code snippet for this:</p>

<pre><code>var stream = process.stdout;

stream.pipe(detector); // Using the first consumer

function startAnotherConsumer() {
    stream2 = new PassThrough();
    stream.pipe(stream2);

    // use stream2 somewhere else
}

function stopAnotherConsumer() {
    stream.unpipe(stream2);
}
</code></pre>

<p>My problem here is that unpiping the stream2 doesn't get it closed. If I call <code>stream.end()</code> after the <code>unpipe</code> command, then it crashes with the error:</p>

<pre><code>events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at writeAfterEnd (_stream_writable.js:192:12)
    at PassThrough.Writable.write (_stream_writable.js:243:5)
    at Socket.ondata (_stream_readable.js:555:20)
    at emitOne (events.js:101:20)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at Pipe.onread (net.js:548:20)
</code></pre>

<p>I even tried to pause the source stream to help the buffer to be flushed from the second stream but it didn't work either:</p>

<pre><code>function stopAnotherConsumer() {
    stream.pause();
    stream2.once('unpipe', function () {
        stream.resume();
        stream2.end();
    });
    stream.unpipe(stream2);
}
</code></pre>

<p>Same error as before here (write after end).</p>

<p>How to solve the problem? My original intent is to duplicate the streamed data from one point, then close the second stream after a while.</p>

<blockquote>
  <p>Note: I tried to use <a href="https://stackoverflow.com/a/19561718/1531853">this answer</a> to make it work.</p>
</blockquote>