I have a Sinatra app with a long running process (a web scraper). I'd like the app flush the results of the crawler's progress as the crawler is running instead of at the end.
I've considered forking the request and doing something fancy with ajax but this is a really basic one-pager app that really just needs to output a log to a browser as it's happening. Any suggestions?
Update (2012-03-21)
As of Sinatra 1.3.0, you can use the new streaming API:
Old Answer
Unfortunately you don't have a stream you can simply flush to (that would not work with Rack middleware). The result returned from a route block can simply respond to
each
. The Rack handler will then calleach
with a block and in that block flush the given part of the body to the client.All rack responses have to always respond to
each
and always hand strings to the given block. Sinatra takes care of this for you, if you just return a string.A simple streaming example would be:
Now you could simply place all your crawling in the
each
method: