4 Live Stream rails do not work with Puma

I am trying to do a little test on rails ActiveController::Livewith a Puma server. I started the Puma server on rails s pumaand used it curl localhost:3000/messages/eventsfor testing. However, before the data was returned immediately, there was a long pause, which was the same as using WEBrick. So why is the Puma server not broadcasting the results?

class MessagesController < ApplicationController
  include ActionController::Live

  def index
    @messages = Message.all
  end

  def create
    @message = Message.create!(params[:message].permit(:content, :name))
  end

  def events
    3.times do |n|
      response.stream.write "#{n}...\n\n"
      sleep 2
    end
  ensure
    response.stream.close
  end
end
+4
source share
1 answer

You need to set the response headers

def events
   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
+1
source

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


All Articles