Search code examples
chicagoboss

Need an use case example for stream response in ChicagoBoss


ChicageBoss controller API has this

{stream, Generator::function(), Acc0}

Stream a response to the client using HTTP chunked encoding. For each chunk, the Generator function is passed an accumulator (initally Acc0) and should return either {output, Data, Acc1} or done.

I am wondering what is the use case for this? There are others like Json, output. When will this stream be useful?

Can someone present an use case in real world?


Solution

  • Serving large files for download might be the most straight-forward use case.

    You could argue that there are also other ways to serve files so that users can download them, but these might have other disadvantages:

    • By streaming the file, you don't have to read the entire file into memory before starting to send the response to the client. For small files, you could just read the content of the file, and return it as {output, BinaryContent, CustomHeader}. But that might become tricky if you want to serve large files like disk images.
    • People often suggest to serve downloadable files as static files (e.g. here). However, these downloads bypass all controllers, which might be an issue if you want things like download counters or access restrictions. Caching might be an issue, too.