messense/leptos_sse

Use multiple SSE event streams

hirschenberger opened this issue · 1 comments

It would be nice if one could open multiple SSE streams by using a path extractor in the SSE handler for each event type. So the diffing of the payload is more efficient.

like this:

pub async fn handle_sse(
    Path(event): Path<Cow<'static, str>>,
) -> Sse<impl Stream<Item = Result<Event, axum::BoxError>>> {
    let state = BACKEND_STATE.read();
    let stream = ServerSentEvents::new(
        event,
        BroadcastStream::new(state.sse_rx.resubscribe()).map_err(|e| e.into()),
    )
    .unwrap();
    Sse::new(stream).keep_alive(KeepAlive::default())
}

Each component could provide it's own stream in it's context.
Unfortunately here only the existence of the ServerSignalEventSource type is checked in the context and creation of a new stream is skipped if it already exists.

Would be cool if we also compare the URL and create a new stream in the context, if the URL is different.

It seems ServerSentEvents::new only takes one event name and Sse::new only takes one ServerSentEvents. Is it even possible to create a stream sending multiple events? You mentioned it's inefficient but I think it's actually impossible, or at least I haven't figured out how to do it.