4

I am building a video streaming server in nodeJS using express and using "request-progress" module to get the progress status. It's(video streaming) working fine.
But the problem is, even after I close the browser or moves to next page, the server still streaming data. I can see that in console.

Here is the code for the same:

app.route('/media/*').get(function (req, res) {

  var originalUrl = "http://myactualserver.com";
  var resourceUrl = originalUrl.split('media');
  var requestURL = BASE_URL + resourceUrl[1];

  req.on('close', function () {
    console.log('Client closed the connection');
  });

  var options = {};
  progress(request(requestURL), {
    throttle: 1000,
    delay: 0,
    lengthHeader: 'content-length',
  })
    .on('progress', function (state) {
      console.log('progress', state);
    })
    .on('error', function (err) {
      console.log('err');
    })
    .on('end', function () {
      console.log('end');
    })
    .pipe(res);
});

I tried the following post and finally added the

req.on("close", function(){});


Video Streaming with nodejs and Express

node.js http server, detect when clients disconnect

Node.js server keeps streaming data even after client disconnected

My question is:
1. How will call either the "error" or "end" function of "request-progress"?
2. How will I close the streaming?
3. Why console.log('Client closed the connection'); is called twice?

Community
  • 1
  • 1
Uday Shankar Singh
  • 501
  • 1
  • 4
  • 15

1 Answers1

2

Answers to your questions:

  1. You can probably use emit from events.EventEmitter

Refer this example

https://github.com/IndigoUnited/node-request-progress/blob/master/test/test.js

Alternatively you can use the last progress status in close function.

  1. Not sure what is the question here, the progress will continue till the socket is closed.

  2. If you are making the request from browser (default player), it makes two requests.