Skip to content

Instantly share code, notes, and snippets.

@mcfiredrill
Last active December 12, 2015 04:09
Show Gist options
  • Save mcfiredrill/4712633 to your computer and use it in GitHub Desktop.
Save mcfiredrill/4712633 to your computer and use it in GitHub Desktop.
sinatra streaming
require "sinatra"
get '/' do
erb :index
end
get '/stream', provides: "text/event-stream" do
puts "hi"
stream(:keep_open) do |out|
1000.times do |i|
out << "data: #{i} bottle(s) on a wall...\n\n"
sleep 0.5
end
end
end
<script>
var es = new EventSource('/stream');
es.onmessage = function(e){
console.log(e.data);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment