Skip to content

Instantly share code, notes, and snippets.

@mroth
Created June 24, 2013 21:57
Show Gist options
  • Save mroth/5853993 to your computer and use it in GitHub Desktop.
Save mroth/5853993 to your computer and use it in GitHub Desktop.
Simple SSE example -- works fine locally, connections never close on Heroku. Running at http://obscure-depths-3413.herokuapp.com/
require 'sinatra/base'
class MyApp < Sinatra::Base
set :path, '/tmp'
set :environment, 'production'
def initialize
@connections = []
EM::next_tick do
EM::add_periodic_timer(1) do
@connections.each do |out|
out << "connections: " << @connections.count << "\n"
end
puts "*** connections: #{@connections.count}"
end
end
end
get '/' do
stream(:keep_open) do |out|
@connections << out
puts "Stream opened from #{request.ip} (now #{@connections.size} open)"
out.callback do
@connections.delete(out)
puts "Stream closed from #{request.ip} (now #{@connections.size} open)"
end
end
end
end
require "./app"
$stdout.sync = true
run MyApp
source "https://rubygems.org"
ruby '1.9.3'
gem 'sinatra'
gem 'thin'
gem 'foreman'
GEM
remote: https://rubygems.org/
specs:
daemons (1.1.9)
dotenv (0.8.0)
eventmachine (1.0.3)
foreman (0.63.0)
dotenv (>= 0.7)
thor (>= 0.13.6)
rack (1.5.2)
rack-protection (1.5.0)
rack
sinatra (1.4.3)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
thin (1.5.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thor (0.18.1)
tilt (1.4.1)
PLATFORMS
ruby
DEPENDENCIES
foreman
sinatra
thin
web: bundle exec thin -R config.ru start -p $PORT
@williamkapke
Copy link

Ever figure out a solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment