I have a Sinatra web application that I would really like to improve with streaming updates for certain functions. Right now, however, I'm just trying to learn how to use streaming data, which I have never done before. I have the following simple test code:
In Sinatra:
get '/foo' do stream do |out| 10.times do out.puts "foo" out.flush sleep 1 end end end get '/bar' do erb :bar end
In bar.erb
:
<body> <div class="stream"> nothing. </div> </body> <script type="text/javascript" charset="utf-8"> $(document).ready( function() { $.get('/foo', function(html) { $(".stream").html(html); }); }); </script>
I am not surprised that this does not do what I want to match each "foo" when it is recording and refreshing the page dynamically. Instead, nothing happens for ~ 10 seconds, and then I get foo foo foo foo foo foo foo foo foo foo foo
.
My question is: how in the ERB template (using Ruby, jQuery or other means) can I pull out the streaming data as provided, instead of blocking until they all come together and spit it all out at once?
source share