7

I'm trying to implement a small test on rails ActiveController::Live with Puma server. I started Puma server by rails s puma, and used curl localhost:3000/messages/events to do test. However there was a long pause before the data is returned all at once, which was the same as using WEBrick. So Why doesn't the Puma server live stream 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
zishe
  • 10,665
  • 12
  • 64
  • 103

1 Answers1

0

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
KPheasey
  • 1,765
  • 16
  • 22