You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, to return an SSE response the tokio feature is required, even though Tokio runtime is only needed for timeouts to maintain keep-alive connection. This limitation restricts the use of SSE with other runtimes.
Proposal
I propose to remove the strict dependency of tokio feature for SSE and leave it only for keep-alive events.
This would be useful in cases such as:
When axum is used with a different runtime.
When keep-alive events aren't needed. For example I know that messages from my stream are sent frequently enough.
In addition to the proposal, I have an implementation. I explored several other implementation options and eventually came up with a design that I believe is the simplest and also allows for easy customization of keep-alive event handling for arbitrary runtimes:
// The `Sse` type is now accessible without the `tokio` feature.pubstructSse<S>{stream:S,}impl<S>Sse<S>{pubfnnew(stream:S) -> Self{/**/}// The `keep_alive` method now wraps an event `Stream`.// The `KeepAliveStream` type implements `Stream` and produces// keep-alive events at the specified interval.// This method will also require the feature flag.#[cfg(feature = "tokio")]pubfnkeep_alive(self,keep_alive:KeepAlive) -> Sse<KeepAliveStream<S>>{Sse{stream:KeepAliveStream::new(keep_alive,self.stream),}}}
This design preserves the familiar way of using SSE:
use axum::response::sse::{Event,KeepAlive,Sse};asyncfnsse_handler() -> Sse<implStream<Item = Result<Event,Infallible>>>{let stream = /* create the event stream */;Sse::new(stream).keep_alive(KeepAlive::new().interval(std::time::Duration::from_secs(1)).text("keep-alive-text"),)}
Alternatives
As an alternative, we could leave the current SSE implementation as it is and require users to implement their own equivalent for the runtime they are using.
However, this approach requires a significant amount of additional code, much of which is unrelated to the runtime itself (and in some cases, completely unrelated). At the same time, it would be nice to have higher-level support for SSE built into the framework.
The text was updated successfully, but these errors were encountered:
Feature Request
Make server-sent events less dependent on the Tokio feature
Motivation
Currently, to return an SSE response the
tokio
feature is required, even though Tokio runtime is only needed for timeouts to maintain keep-alive connection. This limitation restricts the use of SSE with other runtimes.Proposal
I propose to remove the strict dependency of
tokio
feature for SSE and leave it only for keep-alive events.This would be useful in cases such as:
axum
is used with a different runtime.In addition to the proposal, I have an implementation. I explored several other implementation options and eventually came up with a design that I believe is the simplest and also allows for easy customization of keep-alive event handling for arbitrary runtimes:
This design preserves the familiar way of using SSE:
Alternatives
As an alternative, we could leave the current SSE implementation as it is and require users to implement their own equivalent for the runtime they are using.
However, this approach requires a significant amount of additional code, much of which is unrelated to the runtime itself (and in some cases, completely unrelated). At the same time, it would be nice to have higher-level support for SSE built into the framework.
The text was updated successfully, but these errors were encountered: