Middleware for compressing SSE streams #2728
stevenengler
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently tower-http's
CompressionLayer
middleware does not support server-sent event streams due to buffering within the encoder, which causes the SSE events to be delayed. For this reason tower does not apply compression to "text/event-stream" responses.Since the lack of compression is a bit painful when using SSE with htmx (streaming html without compression can use way too much bandwidth), I wrote a small proof-of-concept middleware for applying gzip compression in axum that can be used as a workaround. I'm hoping this helps someone else who wants to apply compression to their SSE streams, since I haven't seen any other examples of it.
and use it with
.layer(axum::middleware::from_fn(compress_sse))
.Beta Was this translation helpful? Give feedback.
All reactions