Created
September 12, 2012 02:28
-
-
Save fedesoria/3703846 to your computer and use it in GitHub Desktop.
stream in sinatra
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
class CheckinServer < Sinatra::Base | |
include Paperclip::Glue | |
set :server, :thin | |
set :views, settings.root + '/../app/views/sinatra' | |
connections = [] | |
get '/stream', :provides => 'text/event-stream' do | |
stream :keep_open do |out| | |
connections << out | |
out.callback { connections.delete(out) } | |
end | |
end | |
post '/new' do | |
user = User.find(params[:id]) | |
connections.each { |out| out << "data: #{erb :user, :locals => {:user => user}}\n\n" } | |
204 # response without entity body | |
end | |
def get_connections_size | |
connections.size | |
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
How can I access get_connections_size from a Rails controller? |
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
Tidy::Application.routes.draw do | |
mount CheckinServer => '/checkin' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment