1
votes

Sorry at first. My English is not good....

I am trying Server Sent Events in Rails4 (Ruby on Rails).

sse_controller.rb

 def test_sse
    response.headers['Content-Type'] = 'text/event-stream'
    sse = SSE.new(response.stream)
    puts 'New SSE = '+sse.to_s

    begin
      loop do
        sse.write({ nowtimes: Time.new}, event: 'timer')
      end
    ensure
      sse.close
    end
 end

mypage.html.erb

$(document).ready(function(){
    var evtSource   = new EventSource("/sse/test_sse");
    evtSource.addEventListener('timer', function(e) {
        obj = JSON.parse(e.data);
        console.log(obj.nowtimes);
    });
})

I use NGINX as my web server.

  1. While I used Passenger as my app server. It's work good but can't support multi-thread. It's only can open 6 Tabs at the same time.

  2. While I try PUMA as my app server, It's work strangely. SSE in client is trying reconnect uninterrupted.

If I use Passenger, how should I resolve the problem about number of connection?

Or if I use PUMA, how should I resolve the loop about reconnect?

1

1 Answers

2
votes

First, based on that I wouldn't use PUMA as the app server. Passenger is behaving correctly: the 6 tab limit is a browser connection limit. If you try with Chrome and Firefox side-by-side you should be able to get 12 connections.

See connections-per-hostname column at http://www.browserscope.org/?category=network&v=1 (and some info on how to customize the connection limit is here: https://stackoverflow.com/a/16855830/841830 )

By the way, shouldn't you have a sleep in your loop do ... end ? Otherwise it will be pumping out SSE messages so fast the browser won't be able to keep up. In this kind of test SSE script, a one-second sleep is normal.