3

I have to maintain a legacy code. The basics: it should handle incoming stream (audio and/or video). The 3rd party device send the stream when it's requested. Then a client should consume it.

The code was unfinished, but for me, seems good. NodeJS version is the latest (atm): 8.9.4

// init.js (the main js file)

global.readStream = new readStream();

The read stream object look like this old one: // readStream object

var Readable = require('stream').Readable;
var util = require('util');

function myReadStream() {
  Readable.call(this);
  this.data = null;
}
util.inherits(myReadStream, Readable);

myReadStream.prototype.addData = function (newData) {
    this.data = newData;
    console.log('new data: ', this.data);
};

myReadStream.prototype._read = function () {
  this.push(this.data);
};

So, the two endpoint:

app.get('incomingdata', function (req, res) {
     myReadStream.addData = res.newIncomingData; 
     // just writing the stream data directly
     console.log('incoming: ', res.newIncomingData);
});

app.get('outgoingData', function (req, res) {
     myReadStream
  .on('readable', function () {
    var obj;
    while (null !== (obj = myReadStream.read())) {
      myReadStream.pipe(res); // direct pipe to res
      // alternative idea: res.send(obj);
      // alternative #2: res.write(obj);
      console.log('outgoing: ', obj);
    }
  });
});

The result is, the call of "outgoingData" always repeat just one stage of the readStream data (what was recently feeded before the outgoing data call) but not refreshing...

E.g.:

Incoming: <Buffer 00 e3 ff ab a2 ....>
new data: <Buffer 00 e3 ff ab a2 ....>
Incoming: <Buffer 01 3b ca c1 b0 ....>
new data: <Buffer 01 3b ca c1 b0 ....>
Outgoing: <Buffer 01 3b ca c1 b0 ....>
Incoming: <Buffer 99 fa e8 77 00 ....>
new data: <Buffer 99 fa e8 77 00 ....>
Outgoing: <Buffer 01 3b ca c1 b0 ....>
Incoming: <Buffer ef b0 00 22 33 ....>
new data: <Buffer ef b0 00 22 33 ....>
Outgoing: <Buffer 01 3b ca c1 b0 ....>
Incoming: <Buffer 25 7b 91 aa 00 ....>
new data: <Buffer 25 7b 91 aa 00 ....>
Outgoing: <Buffer 01 3b ca c1 b0 ....>

I only have experience with Socket.Io, not much in readable and writeable. As far as I know, this code was developed ~1.5y ago, for version 4.x. The code seems good, but miss something. Any idea what's wrong, how can I correct it?

(p.s.: I don't like to maintain legacy code)

huncyrus
  • 648
  • 8
  • 18

0 Answers0