public
Created

Simple SSE example -- works fine locally, connections never close on Heroku. Running at http://obscure-depths-3413.herokuapp.com/

  • Download Gist
Gemfile
Ruby
1 2 3 4 5 6
source "https://rubygems.org"
ruby '1.9.3'
 
gem 'sinatra'
gem 'thin'
gem 'foreman'
Procfile
1
web: bundle exec thin -R config.ru start -p $PORT
app.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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
config.ru
Ruby
1 2 3 4
require "./app"
 
$stdout.sync = true
run MyApp

Ever figure out a solution to this?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.