Closed kmvignesh closed 1 year ago
Hi @kmvignesh can you please provide the code you use to setup the subscription.
Also, you mentioned you are unable to reproduce Appsync error 5, but that the issue happens when you start emulator go offline and then go online. What error or issue is consistently happening after?
Is this issue the same as your previous one? https://github.com/aws-amplify/amplify-flutter/issues/2581
Wondering if this could somehow be related as well: https://github.com/aws-amplify/amplify-flutter/issues/2599
Hi @kmvignesh can you please provide the code you use to setup the subscription.
Also, you mentioned you are unable to reproduce Appsync error 5, but that the issue happens when you start emulator go offline and then go online. What error or issue is consistently happening after?
Is this issue the same as your previous one? #2581
@fjnoyp yes. this issue is similar to https://github.com/aws-amplify/amplify-flutter/issues/2581 . But the main issue is, when user moves from background to fore ground we cancel the subscription like mentioned here. But when these exception is thrown, re-subscription not happening.
Okay thanks for clarifying that. Can you please provide your code for subscribing and re subscribing then. After I can test on an iOS simulator to see if I can replicate.
@fjnoyp Please find the code below.
try {
_stream = Amplify.API.subscribe(
GraphQLRequest<String>(
apiName: api,
document: getDocument(),
variables: getVariables()),
onEstablished: () {
log('AppSync connection established');
},
);
} on ApiException catch (e, s) {
crashlytic.log(exception: e, stackTrace: s)
}
_Subscription = _stream?.listen((event) {
handleData(event, syncReading);
}, onDone: () {
log('AppSync connection closed');
}, onError: (e, s) {
crashlytic.log(exception: e, stackTrace: s)
});
Hi @kmvignesh where is the resubscription logic triggered in your code?
Based on the exception you get on resubscribe:
ApiException(message: Subscription item event failed with error: Unauthorized
It appears this might be an issue with your auth setup.
In your amplify config the authorization is set to OIDC. In this case you need to configure and manage the details of the token refreshes yourself - can you verify that you have done this properly?
Perhaps the app has been backgrounded long enough for the token to expire and your token is not being refreshed on app foreground before you attempt to resubscribe.
When the app goes to background we cancel the subscription and re-subscribe it on resume using lifecycle handler.
class LifecycleHandler extends WidgetsBindingObserver {
@override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
AppSyncDal().subscribe();
break;
case AppLifecycleState.paused:
AppSyncDal().cancel();
break;
}
}
}
class AppSync {
Stream<GraphQLResponse<String>>? _stream;
StreamSubscription<GraphQLResponse<String>>? _subscription;
void cancel() {
if (_stream != null) {
_subscription?.cancel().then((value) {
log('AppSync cancelled');
});
_stream = null;
}
}
void subscribe() {
try {
_stream = Amplify.API.subscribe(
GraphQLRequest<String>(
apiName: api,
document: getDocument(),
variables: getVariables()),
onEstablished: () {
log('AppSync connection established');
},
);
} on ApiException catch (e, s) {
crashlytic.log(exception: e, stackTrace: s)
}
_subscription = _stream?.listen((event) {
handleData(event, syncReading);
}, onDone: () {
log('AppSync connection closed');
}, onError: (e, s) {
crashlytic.log(exception: e, stackTrace: s)
});
}
}
Below is the code for our auth setup
AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
AmplifyAPI amplifyAPI = AmplifyAPI(
authProviders: const [
CustomOIDCProvider(),
],
);
await amplify.addPlugins([authPlugin, amplifyAPI]);
String config = '...';
await amplify.configure(config);
class CustomOIDCProvider extends OIDCAuthProvider {
const CustomOIDCProvider();
@override
Future<String> getLatestAuthToken() async {
String token = await getAuth().getToken();
return token;
}
}
Thanks for the info, I was able to reproduce the errors you said you were able to reproduce (not the unauthorized errors). From what I understand of the underlying amplify-swift libraries these are expected when the connection becomes unstable and at times, you will have to resubscribe. You may also be able to use something like connectivity_plus to monitor the user's connection and cancel when they lose network, however, there will probably still be sometimes when you will have to resubscribe.
Regarding the unauthorized errors you mentioned, when you say resubscribe does not work, do you mean resub fails bc unauthorized or it's not possible to try? I know you said you can't repro this so maybe tough to know, but the most likely explanation would be that the auth token from CustomOIDCProvider
isn't valid or isn't working. Is there any way to determine if these tokens are valid and getting pulled in correctly in these situations? If that's not working, then the unauthorized messages would be expected and it won't be possible to establish the connection.
Regarding error messages 5, 4, 3, also not sure what they mean, will update here if I get a definitive answer.
@ragingsquirrel3 We are using connectivity_plus
plugin as well as InternetAddress
to make sure internet is available before making any subscription. Still we are getting these issues.
Hi @kmvignesh thanks for you reply.
That's good that you check for internet availability before making any subscription.
However, it seems the re subscription is still not working.
Therefore, let's analyze if unauthorized errors are causing re subscription to not work.
A most likely explanation would be that the auth token from CustomOIDCProvider isn't valid or isn't working.
Can you determine if these tokens are valid and getting pulled in correctly when you re subscribe?
If you aren't pulling proper auth tokens, then the unauthorized messages would be expected and it won't be possible to establish re subscribe and establish that connection.
@kmvignesh - If you are still facing this issue can you please provide the info request above? Thanks.
Hi @kmvignesh since we haven't heard back I'm going to close this issue. Please feel free to create another or respond here should you need any more help.
Description
We are getting these errors while listening the stream's
onError
callback when user has un-stable internet.and
We where not able to find/re-produce below error. But when we get this exception with
error 5
app-sync re-subscription also not working. Only killing and relaunching the app is working.Can anyone explain what does it mean by error 5,4 and error 3.
Categories
Steps to Reproduce
Screenshots
No response
Platforms
Android Device/Emulator API Level
No response
Environment
Dependencies
Device
iPhone XR
OS
iOS 15.7.1 & iOS 16.2
Deployment Method
Amplify CLI
CLI Version
No response
Additional Context
No response
Amplify Config
{ "UserAgent": "aws-amplify-cli/2.0", "Version": "1.0", "auth": {..}, "api": { "plugins": { "awsAPIPlugin": { "app-name": { "endpointType": "GraphQL", "endpoint": "https://mydomain.amazonaws.com/graphql", "region": "region", "authorizationType": "OPENID_CONNECT" } } } } }