apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.76k stars 653 forks source link

Automatic Websocket Reconnection #895

Closed Syl1234 closed 6 years ago

Syl1234 commented 6 years ago

I sucessfully managed to use ApolloClient in order to deal with GraphQL Subscription over websocket. I can see events onNext happening. Unfortunatly when my server is facing difficulties, ApolloClient is not able to reconnect using the same websocket.

`

 private ApolloSubscriptionCall<Subscription.Data> getMySubscriptionCall() {
    Subscription = Subscription.builder().build();
    return apolloClient.subscribe(Subscription);
}

/ /

    okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new LoggingInterceptor())
            .pingInterval(30, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .build();

    apolloClient = ApolloClient.builder()
            .serverUrl(env.getBaseUrl())
            .okHttpClient(okHttpClient)
            .subscriptionTransportFactory(new WebSocketSubscriptionTransport.Factory(env.getSubscriptionBaseUrl(), okHttpClient))
            .build();

    disposables.add(Rx2Apollo.from(getMySubscriptionCall())
            .subscribeOn(Schedulers.io())
            .subscribeWith(new Subscriber()));

`

Is there any other particular configuration that i must add in order to reconnect the client ?

Thx for advices

sav007 commented 6 years ago

Have you tried to unsubscribe and then subscribe after some delay like 10-20 seconds? After 10 sec timeout we will kill web socket connection.

sav007 commented 6 years ago

Actually I guess we need to add support of heart beat from server side: https://github.com/apollographql/subscriptions-transport-ws/blob/7482425bb27139d99f45c88ac46c7223c210fbf9/src/message-types.ts#L7