Skip to content

custom nodejs duplex stream with single reader and multiple writers #269

Closed
@pps83

Description

@pps83

My script makes a GET http request and saves received data into a Buffer.
Then this script listens for incoming connections and when they are established sends out what's stored in the Buffer.

If it was synchronous pseudo-code it would be like this:

// single reader, all data loaded into `buf`
var buf = http.get('http://example.com/movie.mp4');
app.use('/movie.mp4', function(req, res, next){
  res.end(buf);  // multiple writers
  next();
));

Here's what I want to do now: I want to be able to serve 'multiple writers' while 'single reader' is still reading data, basically when I get 'data' event for 'single reader' I'd store it to a buffer and pipe it to all connected 'multiple writers'. While all this happens new writers could be accepted, for them I'd need to first send whatever had been buffered so far into buf and when it's finished all new 'data' events on the 'single reader' would also be piped to these new writers. All the data to be piped to 'multiple writers' is always buffered inside buf, that is I'd prefer to implement it so that there is no needless copying or buffering going on, it's always fully buffered inside buf

How can I write that in nodejs's streams?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions