Wednesday, 7 June 2017

How can i get client downloading speed in node js

I have build server in node js that is using after authorization to download file using this code:

app.get('/file/:user/:pass', function(req, res) {
                            /* HEADER */
                            res.writeHead(200, {
                                'Content-Disposition': 'attachment;filename='+stream+'.ts',
                                'Content-Type': 'video/mp2t',
                                'Cache-Control': 'no-cache'
                            });

/* GET - last  file */

fs.readFile('/root/myfile.ts', 'utf-8', function(err, data) {

       /* WRITE - stream to RAM */
       buffer[stream] =(fs.readFileSync('/root/myfile.ts'));

      /* STREAM - client */
      res.write(buffer[stream]);
});

This is cut code from my streaming file to client using http protocol...i need to get client download speed from server so does someone have idea how to get download speed?

First idea is to use setTimeOut function that is every second reading read bytes that is send to client...this is not good because if i have 10 000 users reading file it will have 10 000 setTimeOut events and CPU usage will be very high...does anyone have idea or example code how to get download speed?

Like for example:

var client_speed = req.connection.readBytes;

And then converting readBytes to Mbps...or is there Node function or libaray that automatically calculate or get client download speed when res.write function is executed?



via John

No comments:

Post a Comment