Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are subscription updates supported? #37

Closed
ghost opened this issue May 23, 2022 · 2 comments
Closed

Are subscription updates supported? #37

ghost opened this issue May 23, 2022 · 2 comments

Comments

@ghost
Copy link

ghost commented May 23, 2022

First time a subscription is executed on the frontend it returns data and works fine but I'm unable to get any updates.

Reading the library code it looks like data from subscription handler stream is only read and emitted once after which the function returns, initially I thought that I must make my streams to never return None but instead that lead to the whole program freezing. I'm pretty new to rust and juniper but to me it seems like these currently implemented "subscriptions" are really more one-off events and updates are not supported. Is this correct?

@JonasKruckenberg
Copy link
Owner

JonasKruckenberg commented May 23, 2022

If you look a this line you can see that the plugin in fact reads and emits the stream items asynchronously so yes subscriptions are fully supported.

What you meant is that the handler returns only once, but it returns a stream! The example in this repo is maybe a bit misleading in this regard, you would never construct a stream from a vec in real code. But you can construct a stream from a tokio::mpsc channel and suddenly this becomes much more useful.

use async_stream::wrappers:: ReceiverStream;
use tokio::time::{sleep, Duration};

#[graphql_subscription(context = GraphQLContext)]
impl Subscription {
    async fn hello_world() -> StringStream {
       let (tx,rx) = tokio::mpsc::channel(16);

       tokio::spawn(async move {
          for i in 0..10 {
             if let Err(_) = tx.send(i).await {
                println!("receiver dropped");
                return;
             }
             sleep(Duration::from_millis(250)).await;
           }
       });
   Box::pin(ReceiverStream::new(rx))
    }
}

The above snippet will (hopefully) produce 10 subscription updates spaced 250ms apart to make the effect more noticeable.

@ghost
Copy link
Author

ghost commented May 25, 2022

Okay I see it was a problem with my subscription stream logic. I got it working now, thanks for pointing me in the right direction. Great library btw.

@ghost ghost closed this as completed May 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant