title: Express, Nodejs - How to detect pausing/slow internet connection from client side?
body: <p>I'm using express for downloading processed data to client by using <code>res.write</code> and <code>res.end</code> methods.</p>
  <p>So far everything is working very well, expect if end user has very slow internet connection or is pausing the download while the processed data is still streaming which cause to leak memory after some time.</p>
  <p>An example situation:</p>
  <p>Let assume 10GB data needs to be sent to user (by using chunk + req.write). User decides to pause the download (in browser) or is just having slow internet connection, the file is still being streamed at server-side, but instead sending to user, it is being stored on memory until the user has resumed the download again. Which causes to memory leak.</p>
  <p>Example pseudo code how to cause the memory leak issue if user decide to pausing downloading file:</p>
  <pre><code>app.get(&quot;/download/file&quot;, (req, res) =&gt; {
      res.set({
          &quot;Content-Type&quot;: &quot;any-mime-type&quot;,
          &quot;Content-Disposition&quot;: `attachment;filename=example-file.dat`,
          &quot;Content-Length&quot;: 9999999
      })
  
      const recursive = ()=&gt;{
          const data_chunk = processing_data(req, ..., options);
          req.write(data_chunk);
  
          if (data_transfer_completed){ //if the requirement has been fulfilled
              res.end();
          }else{
              setTimeout(recursive, 50);
          }
      }
  
      recursive();
  })
  </code></pre>
  <p>Now the question is, is there a way to get still-needs-to-be-sent content size from req.write? So then I could build a functionality as example if the content, which still needs to be sent to user. It is over 50MB? Then slow it down / pause the function.</p>
  <p>I thought that req.on(&quot;pause&quot;, function(){}) would work, but I assume this only triggers when using req.pause(). But again, this wouldn't be a solution anyway since slow internet connection doesn't trigger the &quot;pause&quot; functionality (du'h!).</p>
  <p>Is there any solution for that?</p>
  <p>Many thanks,</p>
  <p>Chinafreak</p>