How to get ActionController :: Live streaming to work with Thin?

Question

Can you use thin with ActionController::Live to implement server-side events (SSE) and long polling? If so, how?

Context

Although the name is a repetition. How do I get the Rails 4 ActionController :: Live Broadcast, working with Thin and Ruby 2? And how do Tin and Puma scale live? , OP tormented the water by asking two questions, and this question has not received an answer.

Several other posts suggest that you can use thin for server side events (sse) if you run it through exec thin start --threaded : Does Heroku support ActionController :: Live? and Are puma the only multi-threaded rails of 4 http servers? , Aaron seminal http://tenderlovemaking.com/2012/07/30/is-it-live.html and Ryan are consistently reliable http://railscasts.com/episodes/401-actioncontroller-live?view=asciicast . But even if I copy the Railscast example, I was not able to get it to work with thin .

What i tried

 # ---------------------------------------------------------------- # file: config/routes.rb Rails.application.routes.draw do resources :widgets do collection do get 'events' # SSE test end end end 

_

 # ---------------------------------------------------------------- # file: config/environments/development.rb Rails.application.configure do ... snip ... # see http://tenderlovemaking.com/2012/07/30/is-it-live.html config.preload_frameworks = true config.allow_concurrency = true end 

_

 # ---------------------------------------------------------------- # file: app/controllers/widgets_controller.rb class WidgetsController < ApplicationController include ActionController::Live # GET /widgets/events # see http://railscasts.com/episodes/401-actioncontroller-live?view=asciicast def events # SSE expects the `text/event-stream` content type response.headers['Content-Type'] = 'text/event-stream' 3.times do |n| response.stream.write "#{n}...\n\n" sleep 2 end ensure response.stream.close end end 

_

 # ---------------------------------------------------------------- # Gemfile source 'https://rubygems.org' gem 'rails', '4.1.8' gem 'pg' ... snip ... gem 'thin' 

Launch

In the shell A window:

 $ bundle install Chalcedony[~/Projects/heroku-sample/widget-worker]$ thin start --threaded --trace Using rack adapter Thin web server (v1.6.3 codename Protein Powder) Tracing ON Maximum connections set to 1024 Listening on 0.0.0.0:3000, CTRL+C to stop 

Then in shell window B:

 $ curl --no-buffer localhost:3000/widgets/events 

In the shell A window, I see a request and responses that spit out at intervals of one second (there is one second delay between 0... and 1... and 2... ). It's good:

 GET /widgets/events HTTP/1.1 User-Agent: curl/7.37.1 Host: localhost:3000 Accept: */* HTTP/1.1 200 OK X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff Cache-Control: no-cache Content-Type: text/html; charset=utf-8 X-Request-Id: 95e64eb6-ee21-4e97-a33a-dbf579b3027c X-Runtime: 0.066925 Connection: close Server: thin 0... <delay...> 1... <delay...> 2... <delay...> 

But in the shell B window, the printout is delayed and appears immediately. The same thing happens when I view a page in Chrome. Could you configure some settings correctly?

PS :.

 $ rake about About your application environment Ruby version 2.1.4-p265 (x86_64-darwin14.0) RubyGems version 2.2.2 Rack version 1.5 Rails version 4.1.8 JavaScript Runtime JavaScriptCore Active Record version 4.1.8 Action Pack version 4.1.8 Action View version 4.1.8 Action Mailer version 4.1.8 Active Support version 4.1.8 Middleware Rack::Sendfile, ActionDispatch::Static, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007fb0cb4ae1a0>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag Environment development Database adapter postgresql Database schema version 20141213003938 

PPS

Right now you might be thinking, "Why aren't you using puma like everyone else?" Good question. Right now, I cannot build puma pearls on my machine for reasons that I did not understand. And I use thin in most applications deployed in my heroku application, so I like it. If I can not lose weight, I will put more effort into creating puma.

+6
source share
1 answer

Unfortunately, you cannot use AC::Live with Thin . Here Mark explains why and what are the alternatives.

+2
source

Source: https://habr.com/ru/post/979562/


All Articles