How to UnSubscribe GraphQL Apollo Subscription in Kotlin

We are trying to start subscription in onCreate() method in MainActivity and we are able to connect to graphQL subscription which is connected successfully and giving proper response. Below is the code how we connect to graphQL subscription.

return ApolloClient.Builder()
         .httpServerUrl("https://dqt-customer-test.azurewebsites.net/graphql")
         .webSocketServerUrl("wss://dqt-customer-test.azurewebsites.net/graphql")
         .webSocketReopenWhen { e, attempt ->
            if (e is WebSocketReconnectException) {
                true
            } else {
                attempt < 3
            }
      }
        .build()

How to unsubscribe this WebSocket connection when I move to next screen.

we followed the Apollo Client Documentation for applying the subscription but for closing the connection we couldn’t find any method.

Hopefully these docs help more, but if you cancel the coroutine it will terminate a subscription. When no subscription is active, the WebSocket is closed after a configurable timeout.

1 Like