kamal icon indicating copy to clipboard operation
kamal copied to clipboard

Server-Sent Events not working?

Open karls opened this issue 1 year ago • 4 comments
trafficstars

I have a Django app that works just fine locally and on render.com (I'm porting it to use Kamal 2 and Hetzner), but when deployed via Kamal, the SSE part of the app seems to just stop working. Is there anything extra I need to do — and can't seem to locate in the docs — in order to get SSE working?

The response headers for the text/event-stream request are empty. When I run curl --no-buffer 'http://localhost:9000/events/f01657c8-5d4a-44f7-b2dd-facbea21efc9/' I get the expected response with event: stream-open, then keep-alives and so on. But running the same cURL command against the Kamal-deployed app endpoint does not return anything, it just seems to hang.

CleanShot 2024-10-01 at 14 14 20@2x

And relatedly, I can't turn off response buffering in the proxy configuration. Setting response buffering to false results in

ERROR (SSHKit::Command::Failed): Exception while executing on host <ip>: docker exit status: 1
docker stdout: Nothing written
docker stderr: bash: warning: setlocale: LC_ALL: cannot change locale (en_GB.UTF-8)
Error: accepts 1 arg(s), received 2

Seems to be related to https://github.com/basecamp/kamal/issues/956?

karls avatar Oct 01 '24 11:10 karls

Thanks @karls!

I've raised https://github.com/basecamp/kamal/pull/1018 to fix the buffering arguments. I guess it may be that disabling the buffering is what's needed to fix your issue with SSE?

djmb avatar Oct 01 '24 11:10 djmb

Thanks @djmb! I suspect so, yes.

BTW I'm using django-eventstream, which sets the X-Accel-Buffering response header, which some proxies seem to honour. AFAICT that header is used specifically for SSE, streaming responses etc in order to tell the proxy not to buffer the response. Is this something kamal-proxy could/should support?

karls avatar Oct 01 '24 12:10 karls

Yes, definitely something it should support - I've raised https://github.com/basecamp/kamal-proxy/issues/34 to track 👍

djmb avatar Oct 01 '24 14:10 djmb

@karls this is fixed in the proxy now, so SSE responses will work properly in the next release. Thank for reporting the issue!

kevinmcconnell avatar Oct 03 '24 09:10 kevinmcconnell

This is great! Thank you, @kevinmcconnell! 👍🏻

karls avatar Oct 03 '24 16:10 karls

Going to close this as both issues mentioned in my initial message have been addressed in #1018 and basecamp/kamal-proxy#34.

karls avatar Oct 03 '24 16:10 karls