amplify-swift: Amplify.API.subscribe can not be connected to server in the first time launch app

Describe the bug

When user login to our app We config Amplify with below configurations:

 if let url = url {
       do {
            try Amplify.add(plugin: AWSCognitoAuthPlugin())
            try Amplify.add(plugin: AWSAPIPlugin())
            try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
            
            let configuration = try AmplifyConfiguration(configurationFile: url)
            try Amplify.configure(configuration)
            
        } catch {
            // Need to handle error if Amplify init fails
            print("Failed to initialize Amplify with \(error.localizedDescription)")
        }
}

In the first time install app and open app then login successfully then we call Amplify.API.subscribe to our subscription but I only got failed connection and we can not receive any datas from server, the states as below:

  1. connecting
  2. disconnected (when we call cancel)

As normal connection states should be as below:

  1. connecting
  2. connected
  3. disconnected (when we call cancel)

The issue only happen for the first time subcribe. In the second time we successfully connect and get data After relaunch the app and we try Amplify.API.subscribe again, all subscriptions are working fine.

Steps To Reproduce

1. Refresh Install Application
2. Login to app
3. Then call **Amplify.API.subscribe** realtime data
4. For the fist time subscribe the connection does not successfully
5. In the second time we call **Amplify.API.subscribe** for the same realtime event it can connect successfully and return the data correcly

Expected behavior

Amplify.API.subscribe should always connect successfully even the very first time

Amplify Framework Version

2.33.3

Amplify Categories

API

Dependency manager

Swift PM

Swift version

5.10

CLI version

12.12.4

Xcode version

15.2

Relevant log output

<details>
<summary>Log Messages</summary>


INSERT LOG MESSAGES HERE
```

Is this a regression?

No

Regression additional context

No response

Platforms

iOS

OS Version

iOS 16.2

Device

iPhone 11

Specific to simulators

No response

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

@lawmicha

When the error happens we try to perform subscription again its always got failed with the same error. Base on the timing that SDK give log error timeout, its not a timeout actually. It seems the subscription get timeout immediately when I call subscribe again.

By the way could you tell me how long subscription get timeout if can not connect to server ?

To elaborate further, your suggestion works when we downgrade to version 2.28.0, i.e we can re-subscribe when the app is back to the foreground.

However since upgrading to 2.33.3 (and even on 2.37.0) whenever the app goes to the background and returns to the foreground, calling re-subscribe consistently fails with a network timeout error almost immediately. In reality there is no connection error and any mutations sent works.

The error lines when attempting to re-subscribe are shared in https://github.com/aws-amplify/amplify-swift/issues/3831#issuecomment-2311473235

2024-08-27 10:38:25.609249+0800  [APIAppSyncRealTimeClient] [AppSyncRealTimeClient]Failed to send AppSync request start(AWSAPIPlugin.AppSyncRealTimeRequest.StartRequest(id: "13BCE33A-4FAB-4488-B36A-DE17EDFDDDA9", data: "{\"variables\":{\"userId\":\"12fc012c-1893-4b2e-886e-9f9601bc974d\"},\"query\":\"subscription RefreshMockEvent($userId: String!) {\\n  refreshMockEvent(userId: $userId) {\\n    __typename\\n    userId\\n    eventType\\n    message\\n    distance\\n    chargeAmount\\n    detailMessage\\n    data\\n  }\\n}\"}", auth: nil)), error: Error Domain=kNWErrorDomainPOSIX Code=53 "Software caused connection abort" UserInfo={NSDescription=Software caused connection abort}
2024-08-27 10:38:30.600763+0800  [APIAppSyncRealTimeSubscription] [AppSyncRealTimeSubscription-13BCE33A-4FAB-4488-B36A-DE17EDFDDDA9] Failed to subscribe, error: timeout