How to detect an a dropped SSE (server sent event) client? #2264
Unanswered
frederikhors
asked this question in
Q&A
Replies: 2 comments 8 replies
-
Using Drop should work. Can you share your code using that? |
Beta Was this translation helpful? Give feedback.
3 replies
-
I think you chose the wrong channel type for your needs, if I were you I would switch to use tokio::sync::broadcast channel, where you have one sender and multiple receivers, so you would not have problem with removing sender each time when receiver is gone. Eventually that would look something like this: async fn sse_handler(
State(app_state): State<Arc<AppState>>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
let rx = app_state.broadcaster.add_client().await;
let mut mystream = ReceiverStream::<String>::new(rx).map(|res| Ok(Event::default().data(res)));
let stream = async_stream::stream! {
yield Ok(Event::default().data("welcome"));
loop {
yield stream.next().await.unwrap()
}
};
Sse::new(mystream).keep_alive(KeepAlive::default())
} |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In the below code I'm handling sse connections within the function
sse_handler()
. Is there a way to know when that connection is closed so I can remove the client fromBradcaster.inner.clients
?@davidpdrsn, I tried to understand how to use the
Drop
trait technique you suggested here, with no success.REPL: https://www.rustexplorer.com/b/xkqyt2
Beta Was this translation helpful? Give feedback.
All reactions