Search code examples
graphqlapolloapollo-servergraphql-subscriptionsapollo-android

Android apollo subscriptions not working?


I need to integrate subscription in my Android app. The subscription works fine on localhost in graphiql. I have deployed my backend on Heroku. I am using apollo-server and not hasura. My subscriptions are not working for the url given by Heroku but it works fine on localhost. Queries and mutations work fine for both localhost and Heroku url. So I am trying to access my subscription from my Android client. I have kept the base url as my local host. I have configured my Android emulator correctly for localhost and the queries and mutations part works for my Android client but my subscription part is not working.

I have configured my Apollo client for subscription by adding this

.subscriptionTransportFactory(WebSocketSubscriptionTransport.Factory(baseUrl,okHttpClient))

My subscription code looks as follows

val healthConsultationSubscriptionList = GetHealthConsultationSubscription.builder().build()
        apolloClient.subscribe(healthConsultationSubscriptionList).execute(object :
            ApolloSubscriptionCall.Callback<GetHealthConsultationSubscription.Data> {
            override fun onFailure(e: ApolloException) {
                Log.i("datafailure","${e.message} ${e.localizedMessage} ${e.cause}" )
            }

            override fun onResponse(response: Response<GetHealthConsultationSubscription.Data>) {
                Log.i("datais", response.data()?.healthConsultation()?.chiefComplaint().toString() )
            }

            override fun onConnected() {
                Log.i("dataconnected","Connected")
            }

            override fun onTerminated() {
                Log.i("dataterminated","Terminated")
            }

            override fun onCompleted() {
                Log.i("datacompleted","Completed")
            }

        })

But I keep getting an error saying Subscription failed Subscription failed java.net.ProtocolException: Expected HTTP 101 response but was '400 Bad Request'

Also when I use Graphiql for my subscription and replace localhost with the Heroku url for my subscription , I get following error.

enter image description here

Are these two issues which I am facing are related to one another?


Solution

  • So there was no problem from my Code. The issue was with the free tier of Heroku I am using. I tried replacing my subscription with Hasura's Subscription https://hasura.io/learn/graphql/android/subscriptions/1-subscription/ and it works.