1

First of all this one does not work for me. https://github.com/expressjs/compression/issues/56

router.get("/", (req, res) => {
  const head = {
    'Content-Length': 1905484,
    'Content-Type': 'video/mp4'
  };
  res.writeHead(200, head);

  for(let index=0; index<=finalChunkNumber; index++) {                
    let buf = getVideoBuffersFromServerSync(index);
    res.write(buf);
    res.flush();
  }
  res.status(200).send();
}

getVideoBuffersFromServerSync() simply fetches buffer from a server and on the server .mp4 file read from a file stream. With this app, video is not displayed in the browser, until all buffers are fetched and res.send() is reached. Once res.send() is reached browser displays video well. Also tried adding app.use(compression({ level: 0 })) in express routes. Express does real-time streaming if it is reading from a file using fs.createReadStream. So how to make express to write buffers as a real-time response or is there any other HTTP server framework which can achieve these results?

Edit1: Based on jfriend00's comments added range part to the route as follows

const range = req.headers.range;

if(range) {
  const fileSize = 1905484;
  const parts = range.replace(/bytes=/, "").split("-")
  const start = parseInt(parts[0], 10) ;
  const end = parts[1]
            ? parseInt(parts[1], 10)
            : fileSize-1 ;
  const chunksize = (end-start)+1;
  const index = start/chunksize;

  const head = {
     'Content-Range': `bytes ${start}-${end}/${fileSize}`,
     'Accept-Ranges': 'bytes',
     'Content-Length': chunksize,
     'Content-Type': 'video/mp4'
  };
  let buf = getVideoBuffersFromServerSync(index);
  res.writeHead(206,head);
  res.write(buf);
  res.flush();

}

This one makes first partial request but doesn't make subsequent requests. Any reason why? Do I need to use send() instead of write() ?

  • What does the client code look like? Where is this going? Certain clients don't provide any data until it has all been received. Last time I traced through `res.write()` it does send the data right away to the OS so it would more be about the client and whether it waits until the entire request has been received before processing. – jfriend00 Apr 15 '20 at 04:26
  • Also, how in the world can you do `getVideoBuffersFromServerSync()`? There is no synchronous networking in node.js without doing horrible hacks. – jfriend00 Apr 15 '20 at 04:27
  • Also, if this is supposed to go to a video player, my understanding is that the client is going to ask for a byte range and you're just supposed to send a byte range when it is requested, not just dump the whole video. Code example here: [Video stream with node.js](https://medium.com/better-programming/video-stream-with-node-js-and-html5-320b3191a6b6). – jfriend00 Apr 15 '20 at 04:31
  • Another reference: [Streaming a video to an HTML5 video player with nodejs](https://stackoverflow.com/questions/24976123/streaming-a-video-file-to-an-html5-video-player-with-node-js-so-that-the-video-c). – jfriend00 Apr 15 '20 at 04:33
  • @jfriend00 I will go one by one. Client is browser, Browser load a HTML page and video tag makes a request to /api/video route. That route is hidden in the route.js on server. res.write() not sending out received buffers in most surprising part. Look at my full description and my results with `fs.createReadStream`. `getVideoBuffersFromServerSync()` has C++ nodejs binding where this server receives UDP packets from another server, extracts byte array of the file chunk, wrap it into nodejs buffer and pass it on. It works perfectly in my C/C++ app with boost http server. It is synchronous. – Andy Puneri Apr 15 '20 at 23:11
  • @jfriend00 Video is played in the browser with video tag. – Andy Puneri Apr 15 '20 at 23:12
  • @jfriend00 range part come into picture if video is played from the middle or if playback is changed. Target over here is to play video from start to end, when GET request is made by the browser. – Andy Puneri Apr 15 '20 at 23:17
  • Uh, not exactly. Even when playing from the beginning, most video players don't ask for the entire video all at once. They ask for the first byte range and then, as you watch more, they ask for the next byte range and so on... It's very memory inefficient for large videos to read the entire thing at once. – jfriend00 Apr 16 '20 at 00:23

0 Answers0