sinatra/sinatra — Issue #446: Callback/errback in event streaming
Author: andreif
Created: 2012-01-05 16:26:23+00:00
State: open

http://stackoverflow.com/questions/8718860/handling-event-stream-connections-in-a-sinatra-app

There is [a great example of a chat app using Server-Sent Events][1] by Konstantin Haase @rkh. I am trying to run it and have a problem with callbacks (I use Sinatra 1.3.2 and Chrome 16). They seem not to run (e.g. after page reload), and therefore the number of connections is growing.

Also, the connection is closed in 30-60 sec unless one sets a periodic timer to send empty data, as suggested by Konstantin elsewhere.

Can you replicate it? If yes, is it possible to fix these issues somehow? WebSockets work seamlessly in this respect...

```ruby
get '/stream', provides: 'text/event-stream' do
stream :keep_open do |out|
EventMachine::PeriodicTimer.new(20) { out << "data: \n\n" } # added
settings.connections << out
puts settings.connections.count # added
out.callback { puts 'closed'; settings.connections.delete(out) } # modified
end
end
```

```javascript
var es = new EventSource('/stream');
es.onmessage = function(e) { if (e.data != '') $('#chat').append(e.data + "\n") }; // modified
```

[1]: https://gist.github.com/1476463