basecamp / thruster

MIT License
893 stars 29 forks source link

Transfer-Encoding header lost with EventSource #47

Open zhongsheng opened 2 weeks ago

zhongsheng commented 2 weeks ago

bin/rails s works

image

But with thruster bin/thrust bin/rails s, transfer-encoding is missing. This cause a problem, messages returned as a whole text.

Rails 8.0.0.beta1 ruby 3.3.1 (2024-04-23 revision c56cd86388) [arm64-darwin23]

class SseController < ApplicationController
  include ActionController::Live
  def index
  end

  def events
    response.headers['Content-Type'] = 'text/event-stream'

    sse = SSE.new(response.stream, event: "message")

    5.times {
      sse.write({ message: "hello world" })
      sleep 1
    }
  ensure
    response.stream.close
  end

end
3v0k4 commented 2 days ago

I cannot reproduce with:

I see Transfer-Encoding: chunked with and without thruster.

Could you please double check? 🙏

zhongsheng commented 2 days ago

I cannot reproduce with:

  • rails 8.0.0
  • thruster 0.1.8
  • ruby 3.3.5

I see Transfer-Encoding: chunked with and without thruster.

Could you please double check? 🙏

rails new myapp
cd myapp
rails g controller sse index events
# put some code in events method
HTTP_PORT=8088 bin/thrust rails server

Then access http://localhost:8088/sse/events

3v0k4 commented 1 day ago

My bad, I was curling the wrong port 🤦

I think there's some buffering involved with the GzipHandler.

If you change your Rails controller code as follows then it should work:

class SseController < ApplicationController
  include ActionController::Live
  def index
  end

  def events
    response.headers['Content-Type'] = 'text/event-stream'

    sse = SSE.new(response.stream, event: "message")

    5.times {
-    sse.write({ message: "hello world" })
+    sse.write({ message: "hello world" * 1000 })
      sleep 1
    }
  ensure
    response.stream.close
  end
end