Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs - HTTP Range support / Partial file download

I am creating a music web app that streams MP3s that I have stored in MongoDB(GridFS).

My Question: How can I add http range support so that I can start streaming the audio file 1/2 of the way through without having to wait for the buffer.

I know GridFS supports reading for X bytes - X bytes, so basically I just need to know how to get nodejs to understand it only needs bytes X - X.

Thanks!

like image 607
Quinton Pike Avatar asked Jan 02 '12 00:01

Quinton Pike


1 Answers

The client will send a Range header specifying the absolute starting and ending bytes followed by total file length or '*'.

Examples:

  . The first 500 bytes:
   bytes 0-499/1234

  . The second 500 bytes:
   bytes 500-999/1234

  . All except for the first 500 bytes:
   bytes 500-1233/1234

  . The last 500 bytes:
   bytes 734-1233/1234

The server should then return a response code 206 (Partial content) and the Content-Length should be only the amount of data transmitted.

In the case the range is wrong, the server should either return 416 (Requested range not satisfiable) with a Content-Range field of bytes */* or should ignore the range request and return a 200 with the entire body of the file.

The server must also send an Accept-Ranges field with the value of the accepted range unit, in this case bytes. But the range unit can be any custom range unit you want.

Source: rfc2616

like image 120
fent Avatar answered Oct 01 '22 08:10

fent