0

I am receiving a base 64 encode string in my Nodejs server in chunks, and I want to convert it to a stream that can be read by another process but I am not finding how to do it. Currently, I have this code.

const stream = Readable.from(Buffer.from(data, 'base64'));

But this creates a new instance of stream, but what I would like to do it to keep appending to the open stream until no more data is received from my front end. How do I create an appending stream that I can add to and can be read by another process?

--- Additional information -- Client connect to the NodeJS server via websocket. I read the "data" from the payload on the websocket message received.

socket.on('message', async function(res)  { 
    try
    {
        let payload = JSON.parse(res);      
        let payloadType = payload['type'];
        let data = payload['data'];

---Edit -- I am getting this error message after pushing to the stream.

Error [ERR_METHOD_NOT_IMPLEMENTED]: The _read() method is not implemented
    at Readable._read (internal/streams/readable.js:642:9)
    at Readable.read (internal/streams/readable.js:481:10)
    at maybeReadMore_ (internal/streams/readable.js:629:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  code: 'ERR_METHOD_NOT_IMPLEMENTED'
}

This is the code where I am reading it from, and connected to the stream:

const getAudioStream = async function* () {
    for await (const chunk of micStream) {
      if (chunk.length <= SAMPLE_RATE) {
        yield {
          AudioEvent: {
            AudioChunk: encodePCMChunk(chunk),
          },
        };
      }
    }
  };
james Makinde
  • 943
  • 3
  • 17
  • 36
  • Show how the client connects to the NodeJS server and show the code around the stream you are using to read. Generally speaking, the input streams are not meant to be written to unless for 2-way communication, and this doesn't sound like your case. What you probably want is to create a new stream for your own writing purposes. – José Ramírez Oct 22 '22 at 03:46
  • @JoséRamírez Code is added. Client connects via web socket to the server. – james Makinde Oct 22 '22 at 04:09
  • Ok, so what you need to do is create a stream the moment a client connects via the web socket, and keep a reference to that stream inside the onMessage handler. Every time the handler executes, it extracts the relevant data and writes it to the stream. You can learn about streams online, such as [here](https://nodesource.com/blog/understanding-streams-in-nodejs/). – José Ramírez Oct 22 '22 at 04:30
  • Creating your one stream at the start - keep track of it. Then [`.push()`](https://nodejs.org/api/stream.html#readablepushchunk-encoding) data into it as new data arrives. – jfriend00 Oct 22 '22 at 04:31
  • I am getting the error: I did this in my code: const micStream = new Readable(); When the data arrives, I am adding it like this: micStream.push(Buffer.from(audio, 'base64')); It seems it cant be read. Thanks @JoséRamírez – james Makinde Oct 22 '22 at 04:50
  • Implement the read method. It can be empty. `new Readable({ read() {} });` – José Ramírez Oct 22 '22 at 04:57

0 Answers0