Skip to content

Instantly share code, notes, and snippets.

@rbaulin
Created March 6, 2015 18:36
Show Gist options
  • Save rbaulin/383d9060bf1a4c1659fb to your computer and use it in GitHub Desktop.
Save rbaulin/383d9060bf1a4c1659fb to your computer and use it in GitHub Desktop.
#
# (c) Baulin Roman 2015
#
# http://stackoverflow.com/questions/1247125/how-to-get-sinatra-to-auto-reload-the-file-after-each-change
# https://github.com/rtomayko/shotgun
# sse http://www.w3schools.com/html/html5_serversentevents.asp
# sse with sinatra http://stackoverflow.com/questions/5227431/html5-server-sent-events-with-ruby-sinatra
# cool demo https://gist.github.com/rkh/1476463
#
# bundle exec ruby sinatra_sse_reload.rb
require 'sinatra'
pid = Process.fork do
require 'filewatcher'
FileWatcher.new('./render.rb').watch do |filename|
system "curl -G http://localhost:4567/reload"
end
end
set server: 'thin', connections: []
get '/' do
load './render.rb'
html
end
get '/stream', provides: 'text/event-stream' do
stream :keep_open do |out|
settings.connections << out
out.callback { settings.connections.delete(out) }
end
end
get '/reload' do
puts "reload, connections #{settings.connections.count}"
settings.connections.each { |out| out << "data: update\n\n" }
204
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment