user3174541 user3174541 - 4 months ago 28
Node.js Question

Node - how can i pipe to a new READABLE stream?

My app is supposed to take Web Audio streamed from the client, encode it as MP3, before sending it back out to clients via WebSocket.

I can currently encode and pipe to a file like this:

inbound_stream.pipe(encoder).pipe(fs.createWriteStream('audio.mp3'));


And if I already have a file on the server I can do this:

var mp3File = fs.createReadStream('audio.mp3');

mp3File.on('data', function(buffer){
io.sockets.emit('audio', { buffer: buffer });
});


However, I want to access the encoded chunks in real time, and send those out to clients - not write to a file.

What I want is this, effectively:

inbound_stream.pipe(encoder).pipe(newReadStream);

newReadStream.on('data', function(buffer){
io.sockets.emit('audio', { buffer: buffer });
});


I've looked at Duplex and Transform streams, but frankly I am still learning and the Prototyping made my head spin.

How do I do this? Thanks.

Answer

According to documentation readable.pipe(destination[, options]) the destination should be stream.Writable.

What you can do is to implement your own Writable stream:

const Writable = require('stream').Writable;

const myWritable = new Writable({
  write(chunk, encoding, callback) {
    io.sockets.emit('audio', { buffer: chunk});
    callback();
  }
});

inbound_stream.pipe(encoder).pipe(myWritable);

thats all.