Websocket State

Hi,

I’m trying to use some subscriptions, but I believe something is failing in the first connection.
When I try to subscribe, I have a subscription with success but without any value, after 5 minutes I get “websocket is disconnected” and then start to receive the events.
Another thing I notice is subscription without authorization works instantaneity.
Does someone know what happens?

    let url = URL(string: "wss://myServer/graphql")!
    let webSocketClient = WebSocket(url: url, protocol: .graphql_transport_ws)
    let authPayload = ["authorization": APIManager.shared.keychain.getAuthToken()]
    let webSocketTransport = WebSocketTransport(websocket: webSocketClient)
    webSocketTransport.updateHeaderValues(authPayload)

    return webSocketTransport
  }()
subscriptionAPI.getAllPings(result: { requestResult in
      print("🔥⏰ \(requestResult)")
    })

Logs: :fire::alarm_clock: success(nil)

:fire: firstName success(nil)

2023-02-09 12:25:38.311432+0000 YEO[16928:4076625] [] nw_path_necp_check_for_updates Failed to copy updated result (22)

2023-02-09 12:28:16.568009+0000 YEO[16928:4077396] 10.3.0 - [FirebaseAnalytics][I-ACS800014] Cannot get flag for unregistered flag. SDK name, flag name: app_measurement, session_stitching_token_feature_enabled

2023-02-09 12:28:16.595982+0000 YEO[16928:4077396] 10.3.0 - [FirebaseAnalytics][I-ACS800014] Cannot get flag for unregistered flag. SDK name, flag name: app_measurement, session_stitching_token_feature_enabled

2023-02-09 12:28:16.617302+0000 YEO[16928:4077396] 10.3.0 - [FirebaseAnalytics][I-ACS800014] Cannot get flag for unregistered flag. SDK name, flag name: app_measurement, session_stitching_token_feature_enabled

2023-02-09 12:28:16.633992+0000 YEO[16928:4077396] 10.3.0 - [FirebaseAnalytics][I-ACS800014] Cannot get flag for unregistered flag. SDK name, flag name: app_measurement, session_stitching_token_feature_enabled

2023-02-09 12:28:16.651636+0000 YEO[16928:4077396] 10.3.0 - [FirebaseAnalytics][I-ACS800014] Cannot get flag for unregistered flag. SDK name, flag name: app_measurement, session_stitching_token_feature_enabled

“websocket is disconnected”

2023-02-09 12:30:07.815090+0000 YEO[16928:4077992] [] nw_path_necp_check_for_updates Failed to copy updated result (22)

:fire::alarm_clock: success(Optional(YEO.SyncEvent_PINGSubscription.Data(__data: Apollo.DataDict(_data: [“SyncEvent_PING”: AnyHashable([“time”: AnyHashable(“2023-02-09T12:30:12.284Z”), “__typename”: AnyHashable(“SyncEvent_PING”)])], _variables: nil))))

:fire::alarm_clock: success(Optional(YEO.SyncEvent_PINGSubscription.Data(__data: Apollo.DataDict(_data: [“SyncEvent_PING”: AnyHashable([“time”: AnyHashable(“2023-02-09T12:30:17.285Z”), “__typename”: AnyHashable(“SyncEvent_PING”)])], _variables: nil))))

:fire::alarm_clock: success(Optional(YEO.SyncEvent_PINGSubscription.Data(__data: Apollo.DataDict(_data: [“SyncEvent_PING”: AnyHashable([“__typename”: AnyHashable(“SyncEvent_PING”), “time”: AnyHashable(“2023-02-09T12:30:22.286Z”)])], _variables: nil))))

:fire::alarm_clock: success(Optional(YEO.SyncEvent_PINGSubscription.Data(__data: Apollo.DataDict(_data: [“SyncEvent_PING”: AnyHashable([“__typename”: AnyHashable(“SyncEvent_PING”), “time”: AnyHashable(“2023-02-09T12:30:27.288Z”)])], _variables: nil))))

:fire::alarm_clock: success(Optional(YEO.SyncEvent_PINGSubscription.Data(__data: Apollo.DataDict(_data: [“SyncEvent_PING”: AnyHashable([“__typename”: AnyHashable(“SyncEvent_PING”), “time”: AnyHashable(“2023-02-09T12:30:32.288Z”)])], _variables: nil))))

:fire::alarm_clock: success(Optional(YEO.SyncEvent_PINGSubscription.Data(__data: Apollo.DataDict(_data: [“SyncEvent_PING”: AnyHashable([“__typename”: AnyHashable(“SyncEvent_PING”), “time”: AnyHashable(“2023-02-09T12:30:37.290Z”)])], _variables: nil))))

:fire::alarm_clock: success(Optional(YEO.SyncEvent_PINGSubscription.Data(__data: Apollo.DataDict(_data: [“SyncEvent_PING”: AnyHashable([“time”: AnyHashable(“2023-02-09T12:30:42.291Z”), “__typename”: AnyHashable(“SyncEvent_PING”)])], _variables: nil))))

Hi @david, it sounds like there might be some issues with how the auth is being handled on the server side based on you saying it works when Auth is off. The code snippet you provided is what’s in the docs.

If you are using Apollo Server, you might want to check how you’re doing things in onConnect (docs). That is where you would be handling your authentication logic since the header can’t be sent with every subscription ping.

Thank for the help @watson,
Meanwhile, I forgot to update the post.
I change the way I pass the authorization header now I’m using WebSocketTransport.Configuration :

    let webSocketClient = WebSocket(url: url, protocol: .graphql_transport_ws)
    let authPayload = ["authorization": token]
    let configuration = WebSocketTransport.Configuration(connectingPayload: authPayload)
    webSocketTransport = WebSocketTransport(websocket: webSocketClient, config: configuration)

Now works!
Adding the headers like I was doing doesn’t configure the socket connection correctly and somehow makes the connection correctly when retries.
But any way like this is the correct way to do it.

1 Like

Awesome, glad you got this figured out!