sinatra/sinatra — Issue #448: Why does one need periodic timer for keeping event stream open?
Author: andreif
Created: 2012-01-06 10:02:02+00:00
State: open

I am trying to implement push notification using Server-Sent Events and WebSockets (using [websocket-rack][1] gem). While the socket connection is alive for a long time, EventSource needs pushing empty data every 29 sec to keep open. Is it because of HTTP or some other reason? Assuming that server pushes to many clients, it would be nice to avoid meaningless pushing.

```ruby
get '/stream', provides: 'text/event-stream' do
stream :keep_open do |out|
EventMachine::PeriodicTimer.new(20) { out << "data: \n\n" } # required, otherwise the connection is closed in 30-60 sec
settings.connections << out
out.callback { settings.connections.delete(out) }
end
end
```

[1]: https://github.com/imanel/websocket-rack