6

I have a Rails 4.1 application with a simple controller which streams a response:

class ServerSentEventsController < ApplicationController
  include ActionController::Live

  def index
    response.headers['Content-Type'] = 'text/event-stream'
    sse = ServerSentEvent.new(response.stream)

    begin
      loop do
        sse.write(time: Time.now)
        sleep 1
      end
    rescue IOError
      # When the client disconnects, we'll get an IOError on write
    ensure
      sse.close
    end
  end
end

When I add puma to my gemfile and make a request against this route using curl I get a streamed response as expected:

curl -i 'http://localhost:3000/sse'
<!-- truncated headers output -->    
data: {"time":"2014-08-29 05:16:00 +0100"}

data: {"time":"2014-08-29 05:16:01 +0100"}

data: {"time":"2014-08-29 05:16:02 +0100"}

When I switch to thin in my gemfile and make a request the whole thing locks up. I've read in multiple places that thin can handle concurrent requests but I can't seem to make it work.

I'm starting puma simply by running bundle exec rails server. For thin I've tried bundle exec rails server and multiple configurations like bundle exec thin start -a 127.0.0.1 -threaded. Nothing seems to prevent thin from locking up.

How can I get thin to accept concurrent requests?

David Tuite
  • 22,258
  • 25
  • 106
  • 176
  • I have this working just fine over here. Adding the `thin` gem allows you do to `rails s` and you'll see when the server starts it says `Booting Thin`. I'm checking concurrency with `ab` as curl is only doing single requests. – Anthony Nov 05 '14 at 22:03
  • did you maybe forget to use two dashes? `--threaded Call the Rack application in threads [experimental]` – phoet Nov 05 '14 at 22:23
  • hey @David would uploading my sample project and showing you the `ab` output be sufficient here? – Anthony Nov 06 '14 at 14:46
  • @Anthony I personally don't need the answer anymore since the problem was occurring on a test project which I've since abandoned. I'm not sure if it's me or ardavis who get's to award the bounty now. It was ardavis who opened it. – David Tuite Nov 06 '14 at 16:07
  • @phoet I can't remember now. I'd guess that would generate an unexpected option error but I could be wrong. – David Tuite Nov 06 '14 at 16:08
  • you should delete the question then – phoet Nov 06 '14 at 22:53
  • 1
    @phoet I can't find any instructions which say that I should delete my own questions just because I'm no longer personally interested in the answer. If an answer does come along it can still provide value to the community. I'm also not inclined to delete a question that a third-party has opened a bounty on. Can you point me to a meta post or help topic which adds evidence to your claim that I should delete the question? – David Tuite Nov 07 '14 at 12:37
  • if you don't provide further infos, how is anybody ever going to answer this? plus i think it was a user mistake because me and others had thin running in a multithreaded manner whatsoever. – phoet Nov 07 '14 at 16:26
  • 2
    Have you played with your environment as pointed here http://stackoverflow.com/questions/15441302/how-to-get-rails-4-actioncontrollerlive-streaming-working-with-thin-and-ruby-2 ? – Ruy Rocha Nov 11 '14 at 02:21
  • I apologize for not being around to comment on this. I added the bounty. This question was the exact issue I was having. With 'thin' my server would hang up when I tried to use Rails `ActionController::Live. With 'puma' I had no issues, but I had read that thin should work. – ardavis Nov 12 '14 at 17:02
  • The environment @ruyrocha pointed out seems to work, although extremely unfortunate. – ardavis Nov 12 '14 at 17:03

1 Answers1

1

I had the same issue and I had to start the server like this

  bundle exec thin start -a 127.0.0.1 --threaded -e production
dc10
  • 2,160
  • 6
  • 29
  • 46