title: How to send HTTP chunked response to emulate a video stream using Proxygen and Folly?
body: <p>I am writing a HTTP video streaming server based on Facebooks Proxygen. There is no seeking planned. Using the <code>proxygen::ResponseBuilder</code> I am abled to send chunks of a webm encoded video as HTTP response, i.e. chunked transfer encoding is working. My problem is, that Proxygen waits for <code>proxygen::ResponseBuilder::sendWithEOM()</code> before it even sends the response headers. I would like it to actually send the data asap after each call to <code>proxygen::ResponseBuilder::send()</code>. </p>
  
  <p>I tried to run the ResponseBuilder calls from a lambda executed from the EventBaseThread using <code>evb-&gt;runInLoop()</code> and <code>evb-&gt;runInEventBaseThread()</code></p>
  
  <pre><code>using namespace folly;
  using namespace proxygen;
  
  std::thread t([&amp;](){
      EventBase* evb = EventBaseManager::get()-&gt;getExistingEventBase();    
      // send headers ...
      while ( chunks avail. ) {
          //...
          evb-&gt;runInLoop([&amp;](){
              ResponseBuilder(downstream_)
                       .body(std::move(chunk))
                       .send();
          });
          //... 
      }
      // sendWithEOM ...
  });
  t.detach();
  </code></pre>
  
  <p>This code is called from the <code>onRequest()</code> method of my <code>RequestHandler</code>. I tried to call <code>ResponseBuilder::send()</code> without wrapping it into <code>evb-&gt;runInLoop()</code>, but Proxygen v0.25.0 with Folly v0.42.0 is prohibiting calls to <code>ResponseBuilder::send()</code> from another thread using an assert. I removed this assert from here: <a href="https://github.com/facebook/folly/blob/v0.42.0/folly/io/async/EventBase.cpp#L491" rel="nofollow">https://github.com/facebook/folly/blob/v0.42.0/folly/io/async/EventBase.cpp#L491</a>.</p>
  
  <p>Now the emulated streaming is working, but it's crashing, if there are  parallel requests. I guess it was not meant to be used like this, that's what the assert is for. But maybe anyone knows how to use the Proxygen infrastructure properly for my usecase? </p>