javascript - Stream with Sinatra, update destination gradually -


i have sinatra web app i'd enhance streaming updates functions. right now, though, i'm trying learn way around using streaming data, i've never done before. have following simple test code:

in sinatra:

get '/foo'   stream |out|     10.times       out.puts "foo"       out.flush       sleep 1     end   end end  '/bar'   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'm not surprised doesn't want, pick each 'foo' when it's written , update page dynamically. instead, nothing happens ~10 seconds , foo foo foo foo foo foo foo foo foo foo foo.

my question is, how in erb template (using ruby, jquery, or other means) can pull streamed data provided, instead of blocking until it's collected , spitting out @ once?

sinatra actions wrap entire http response cycle - means waits until action finishes before closing request, @ point browser considers data "complete" , "good" use. have created in code above very, slow sinatra action.

the technology looking websockets, supported modern browsers , provide two-way communications channel between each client , server. websocket channel created "upgrading" regular http request. clients don't support web sockets, may emulated using techniques such http long polling (wherein request left open, without response, until there data available - @ point data shunted down response channel, response channel closed, , client expected open new request further data).

you can set in ruby app using eventmachine , em-websocket. alternative socky believe provides javascript client ruby server.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -