1

I have a question about streaming an audio file via a NodeJS server. I'm using the following code:

var http = require('http');
var fs = require('fs');
var filePath = 'media/test.mp3';
var stat = fs.statSync(filePath);

http.createServer(function(request, response) {
    response.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size
    });
    fs.createReadStream(filePath).pipe(response);
})
.listen(3000);

It does work when I ...

But it does not work when I ...

By not working, I mean I can see a pending request ("request is not finished yet!") in Chrome devtools, but the stream sometimes starts only for less than a second, sometimes it doesn't start at all. In the devtools I can see that only 4 KB are loaded (on localhost it's 3.1 MB).

To enable the access from outside, I configured port forwarding on my router, so that requests to port 3000 are forwarded to my computer's internal IP.

For other things than streaming my setup is working, so for example it is possible to call REST routes defined on the server.

EDIT:

Meanwhile, I also tried to do the streaming with PHP instead of NodeJS. But it shows exactly the same behaviour.

Do you guys have an idea what could be the reason? Thank you!

pschild
  • 2,918
  • 1
  • 19
  • 26
  • @raghbendra There seems no relation between the issue and your proposal of using `forever`. – Lorenz Meyer Jan 18 '18 at 12:13
  • Are you trying to play the file in browser or to download the file ? – Malice Jan 18 '18 at 12:23
  • @Malice In this case I'm trying to play it in the browser. I also tried to open the stream in VLC player, but it also doesn't start when accessed from outside. – pschild Jan 18 '18 at 13:24
  • Could you try printing the `range` headers for the req. Browsers usually use it to read media – Malice Jan 18 '18 at 16:03
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests – Malice Jan 18 '18 at 16:04
  • @Malice The range header for the request is `Range:bytes=0-` for both internal and external access. But I could spot a difference when looking at the Network tab in the devtools: `Public IP: Status=200 Type=media Size=4.0 KB` `Internal IP: Status=200 Type=media Size=3.1 MB` So in case of the internal call (by local IP address) it loads about 60% of the whole media file (it's about 5 MB), in case of external call (by public IP) only 4.0 KB... – pschild Jan 18 '18 at 16:17
  • May be chrome deduces by itself looking at the network speed – Malice Jan 18 '18 at 17:31
  • _Malice_ makes a good point by suggesting `range` header. What happens if you change the status code to `206`? – TGrif Jan 18 '18 at 18:02
  • @TGrif When I change the status code to 206 also the internal version does not work anymore (no music is playing). For the external one, nothing changes (also not working). I tried to do the streaming with PHP instead of NodeJS, but that shows the exact same behaviour as I described... so NodeJS can't be the reason.... Do you have any other ideas? Could it have something to do with the port forwarding? UDP/TCP-stuff? – pschild Jan 18 '18 at 19:52

2 Answers2

0

Looks like some configuration issue with setting up port forwarding in router as you have tried with two different code bases.

If you are doing only for testing you can use localtunnel to expose your localhost to publicly over the internet.

There are few other alternatives also like ngrock or forwardhq.

Hope it helps.

vinit payal
  • 1,201
  • 2
  • 13
  • 27
  • 1
    Thank you for the hints. Yesterday, I finally was able to test the exact same setup but with a different router (FritzBox 7320) -- and everything worked as it should. So there must be problems with the router I'm using at home (o2 HomeBox 6441). I will try to get some support in the manufacturer board. – pschild Jan 27 '18 at 09:47
0

Yesterday, I finally was able to test the exact same setup but with a different router (FritzBox 7320) -- and everything worked as it should. So there must be problems with the router I'm using at home (o2 HomeBox 6441).

I think the best will be trying to get some support in the manufacturer board.

Thanks for your effort anyways!

pschild
  • 2,918
  • 1
  • 19
  • 26