tauri-plugin-graphql icon indicating copy to clipboard operation
tauri-plugin-graphql copied to clipboard

end the stream on unsubscribe

Open mbracher opened this issue 1 year ago • 0 comments

In case of an endless stream, I noticed that the listener on the page is removed after pausing a subscription but on the rust side the stream keeps going. The effect is you will only data from one subscription but on the rust side it will keep publishing events for the old subscriptions.

Example of endless stream:

#[Subscription]
impl Subscription {
  async fn hello_world(&self) -> impl Stream<Item = String> {
    info!("rs2js hello_world subscribe");

      let mut value = 0;
      tokio_stream::wrappers::IntervalStream::new(tokio::time::interval(Duration::from_secs(1)))
          .map(move |_| {
              info!(?value, "rs2js hello_world produce");
              value += 1;
              format!("nr: {}", value)
          })

  }
}

I think in the case of a websocket transport the stream is ended because the client will send a 'teardown' message on unsubscribe or in case of page reload the websocket connection is closed which somehow also ends the stream at the rust side.

This pull request solves the case where client unsubscribes (urql pause). see #124 Not solved is the case where you can reload the page in a tauri app. Client will just start a new subscription and stream of old subscription alse keeps sending items.

Client was implemented with Sveltekit and @urql/svelte

mbracher avatar Feb 19 '23 16:02 mbracher