Created
December 14, 2011 23:01
-
-
Save fractaloop/1478989 to your computer and use it in GitHub Desktop.
Rails 3.1 Sinatra streaming question...
This file contains hidden or 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' | |
require 'thin' | |
class Metaserver < Sinatra::Base | |
set server: 'thin' | |
set connections: [] | |
get '/stream', provides: 'text/event-stream' do | |
stream :keep_open do |out| | |
settings.connections << out | |
out.callback { settings.connections.delete(out) } | |
out.errback { settings.connections.delete out } | |
end | |
end | |
post '/stream' do | |
puts "Relay to #{settings.connections.count} streams" | |
settings.connections.each { |out| out << "data: #{params[:msg]}\n\n" } | |
204 # response without entity body | |
end | |
end |
This file contains hidden or 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 'metaserver' | |
Metaverse::Application.routes.draw do | |
match '/stream', :to => Metaserver | |
resource :login, :only => :show | |
resource :explore, :only => :show, :controller => 'explore' | |
root :to => 'login#show' | |
end |
@baronlinden How did you do that? I am mounting it from application.rb
, did you mounted it directly at config.ru
and worked?
You need to add this to your application.rb
config.preload_frameworks = true
config.allow_concurrency = true
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried mounting the Sinatra app as a Rack middleware in my Rails app and that worked. Cheers.