Is Node.js stream Duplex only there for subclassing?

时间:2015-05-04 19:34:30

标签: node.js stream

I have a need in my code to simultaneously push out live streaming data to multiple clients that comes from a single source, but the clients may or may not be all connected to receive the live stream data. So I need to be able to simultaneously write to a file for late receiving clients.

So I was thinking about using Stream.Duplex classes that I can write to as data comes in that can be read by the assigned clients. However, when I create a new Duplex class and try and write data to it or hook the "data" event I get

[Error: not implemented]

errors. So is the Stream.Duplex class only there to subclass? Is there an easier way to use it?

My test code:

var duplex = require( 'stream' ).Duplex;

var myDuplex = new duplex();

myDuplex.on( 'error', function( error ) {
    console.log( error );
} );
myDuplex.on( 'data', function( buffer ) {
    console.log( "Received [%s]", buffer.toString( 'utf8' ) );
} );

// Push the derta
myDuplex.write( new Buffer( "Hello data.", 'utf8' ) );

1 个答案:

答案 0 :(得分:2)

是的,Duplex用于子类化。它意味着一个既可写又可读的流;它没有说明两者之间的任何关系。

您最有可能寻找的是PassThrough TransformTransform是一种更具体的Duplex类型,其中可读部分是根据可写的计算的,PassThroughTransform的简单实现。