Search code examples
iosswiftgraphqlapollo-serverapollo-ios

Subscription Failure Error WSError(type: Starscream.ErrorType.upgradeError message: \"Invalid HTTP upgrade\", code: 400)?


I need to integrate subscription in my iOS 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 iOS client. I have kept the base url as my local host. The queries and mutations part works for my iOS client but my subscription part is not working.

I have configured my Apollo client for subscription by adding this

let httpNetworkTransport = HTTPNetworkTransport(url: URL(string: "http://localhost:5000")!)
        httpNetworkTransport.delegate = self

        let webSocketTransport = WebSocketTransport(request: URLRequest(url: URL(string: "http://localhost:5000")!))


         let splitNetworkTransport = SplitNetworkTransport(
          httpNetworkTransport: httpNetworkTransport,
          webSocketNetworkTransport: webSocketTransport
        )

        return ApolloClient(networkTransport: splitNetworkTransport)

I also tried replacing http with ws as follows

let webSocketTransport = WebSocketTransport(request: URLRequest(url: URL(string: "ws://localhost:5000")!))

Subscription code is as follows

subscription = Network.shared.apollo.subscribe(subscription: GetHealthConsultationSubscriptionSubscription()){
            [weak self] result in
            guard let self = self else {
                return
            }
            switch result {
            case .success(let result):
                debugPrint(result.data?.healthConsultation.chiefComplaint)

            case .failure(let error):
                debugPrint(" Subscription Failure Error \(error)")
            }
        }

But I am getting error from my iOS client as follows

" Subscription Failure Error WSError(type: Starscream.ErrorType.upgradeError, message: \"Invalid HTTP upgrade\", code: 400)"

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

s


Solution

  • So there was no problem from my ios 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/ios/subscriptions/1-subscription/ and it works.