ruby-on-railspumatransfer-encoding

Rails + Puma + Transfer-Encoding: chunked response - supported or not?


Configuration

--

Transfer-Encoding:  chunked

I've been unable to make this work and unable to find a definitive answer: in the above configuration, I want to stream large amounts of data to the client (in the response) - is this supported?

Can anyone show me the light?


Solution

  • The ActionController::Live API offers exactly what you describe: response.stream.write and response.stream.close. (write flushes a chunk automatically; you'll need to do your own buffering if that doesn't work for you.)

    As long as you include ActionController::Live (and note that affects the behaviour of the entire controller, not just one action), you should then be able to write a streaming response with no further effort: you need not, and should not, set any headers etc related to chunking.

    Additional reference: http://tenderlovemaking.com/2012/07/30/is-it-live.html