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

user3174541

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.

UPDATE

The solution below from @Nazar Sakharenko certainly does what I wanted, but the overhead of live encoding seems to make this inpossible, so writing the encoded MP3, and pre-buffering it seems to be the only way (thanks to various people for the suggestion.)

However I still have problems with this approach. New question here:

node.js - create a new ReadStream for a new file, when that file reaches a certain size

Nazar Sakharenko

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;

var buffer = [];
//in bytes
const CHUNK_SIZE = 102400; //100kb

const myWritable = new Writable({
  write(chunk, encoding, callback) {
    buffer += chunk;
    if(buffer.length >= CHUNK_SIZE) {
       io.sockets.emit('audio', { buffer: buffer});
       buffer = [];
    }

    callback();
  }
});

myWritable.on('finish', () => {
   //emit final part if there is data to emit
   if(buffer.length) {
       io.sockets.emit('audio', { buffer: buffer});
   }
});


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

thats all.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can you pipe a readable stream to another readable stream?

From Dev

How can you pipe a readable stream to another readable stream?

From Dev

How can I make a Readable stream from a Writable stream? (Node.js)

From Dev

Node.js: How can I determine whether a readable stream is at the end?

From Dev

How do I implement a basic node Stream.Readable example?

From Dev

How to transform a highland stream into a node readable stream?

From Dev

Node streams: How can a readable stream emit another readable event if it has been drained (end was emitted)

From Dev

How can I convert a readable stream to valid JSON using nodejs?

From Dev

Why can I push into a Readable stream?

From Dev

Why can I push into a Readable stream?

From Dev

How can I split and pipe multiple NAudio stream

From Dev

How do I turn a String into a Readable Stream?

From Dev

How can I extend Node's stream

From Dev

Pipe HTTP response stream to another Readable stream

From Dev

Pipe HTTP response stream to another Readable stream

From Dev

How to pipe a stream using pdfkit with node js

From Dev

How can I list out bracketed text in Linux CLI in a readable format? Apache Geode node names & info

From Dev

How can I make text in meld readable?

From Dev

How can I make text in meld readable?

From Dev

How can I rewrite this query to be more readable?

From Dev

How can I display an array in a readable format?

From Dev

How can I convert timstamp to a readable time?

From Dev

How can I use a node Transform Stream to turn XML into JSON

From Dev

How can I time a pipe?

From Dev

How to create destination path for node.js stream pipe?

From Dev

When does Node Readable Stream of _read() called

From Dev

Node Streams - Listening for unpipe in a Readable Stream

From Dev

Node; default value if readable stream throws an error

From Dev

Node: How can I use pipe and change one file from a multipart

Related Related

  1. 1

    How can you pipe a readable stream to another readable stream?

  2. 2

    How can you pipe a readable stream to another readable stream?

  3. 3

    How can I make a Readable stream from a Writable stream? (Node.js)

  4. 4

    Node.js: How can I determine whether a readable stream is at the end?

  5. 5

    How do I implement a basic node Stream.Readable example?

  6. 6

    How to transform a highland stream into a node readable stream?

  7. 7

    Node streams: How can a readable stream emit another readable event if it has been drained (end was emitted)

  8. 8

    How can I convert a readable stream to valid JSON using nodejs?

  9. 9

    Why can I push into a Readable stream?

  10. 10

    Why can I push into a Readable stream?

  11. 11

    How can I split and pipe multiple NAudio stream

  12. 12

    How do I turn a String into a Readable Stream?

  13. 13

    How can I extend Node's stream

  14. 14

    Pipe HTTP response stream to another Readable stream

  15. 15

    Pipe HTTP response stream to another Readable stream

  16. 16

    How to pipe a stream using pdfkit with node js

  17. 17

    How can I list out bracketed text in Linux CLI in a readable format? Apache Geode node names & info

  18. 18

    How can I make text in meld readable?

  19. 19

    How can I make text in meld readable?

  20. 20

    How can I rewrite this query to be more readable?

  21. 21

    How can I display an array in a readable format?

  22. 22

    How can I convert timstamp to a readable time?

  23. 23

    How can I use a node Transform Stream to turn XML into JSON

  24. 24

    How can I time a pipe?

  25. 25

    How to create destination path for node.js stream pipe?

  26. 26

    When does Node Readable Stream of _read() called

  27. 27

    Node Streams - Listening for unpipe in a Readable Stream

  28. 28

    Node; default value if readable stream throws an error

  29. 29

    Node: How can I use pipe and change one file from a multipart

HotTag

Archive