zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 620 forks source link

Detect when subscription is lost #1188

Open jonbhanson opened 2 years ago

jonbhanson commented 2 years ago

Hi, thanks for a great package!

Is there a way to detect when a subscription is lost? For example, if the app is backgrounded for some period of time, subscriptions lose their connection to the GraphQL server. If events come in after the connection is lost, the client misses those events. When the client comes back into focus, the data is not fresh. I would like to receive a notification from the package that the subscription went down so that I can fetch data from the server to ensure that it is fresh. Is there a way to do this?

Thanks again and keep up the great work!

vincenzopalazzo commented 2 years ago

Mh! good question!

We have some things internally onConnectionLost but I'm not sure that we expose it as a hook, it is something that we can take a look at.

Thanks for brink this problem up.

jonbhanson commented 2 years ago

@vincenzopalazzo thanks for your work on this. Looking forward to the update! This feature will be very useful.

vincenzopalazzo commented 2 years ago

Maybe you can reproduce and same me a ton of time with the following API that we are provided?

https://api.chat.graphql-flutter.dev/graphql

jonbhanson commented 2 years ago

This is a feature request, not a bug report, so I'm not sure how reproducing applies?

vincenzopalazzo commented 2 years ago

Mh! I was thinking that this feature request was connected to some bugs that we had in the ws.

Looks like that is not the case, and the estimation of this issue is after we complete the refactoring of the ws in the library, so it can take a while

jonbhanson commented 1 year ago

Any updates on this? 🙏 Looking forward to this feature!

large commented 1 year ago

I have an issue with the same thing. You cannot present an decent error message to enduser. In Tibbers GraphQL API when no subscription was found, the websocket are disconnected with "There was an error causing connection lost: Bad state: No element" all the time.

I would suggest that an exception was thrown in the listen-loop, like here:

        SubscriptionOptions so =
            SubscriptionOptions(document: getSubscriptionDocument(selHomeId), errorPolicy: ErrorPolicy.all);
        var sub = client.subscribe(so);
        sub.listen((result) {
            if (result.hasException) { //Throw exception connectionLost here
              debugPrint("Exception ${result.exception.toString()}");
              return;
            }
//(....)
SirCorneliusCob commented 8 months ago

+1