Last active
December 12, 2015 04:09
-
-
Save mcfiredrill/4712633 to your computer and use it in GitHub Desktop.
sinatra streaming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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