When we had initially set up Apollo, we used just the network transport. But subscriptions is an advanced use case which needs more configuration. So we have to add ApolloWebSocket. Add apollowebsocket.framework and starscream.framework from your Carthage/Build/IOS to libraries.

Now we need to update our ApolloClient instance to point to the subscription server.

Open Todo/NetworkManager.swift and update the following:

import Foundation
import Apollo
class NetworkManager {
static let shared = NetworkManager()
let graphEndpoint = ""
+ let graphWSEndpoint = "wss://"
var apolloClient : ApolloClient?
private init (){
func setApolloClient(accessToken: String){
self.apolloClient = {
let authPayloads = ["Authorization": "Bearer \(accessToken)"]
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = authPayloads
+ let map: GraphQLMap = authPayloads
+ let wsEndpointURL = URL(string: graphWSEndpoint)!
let endpointURL = URL(string: graphEndpoint)!
+ var request = URLRequest(url: wsEndpointURL)
+ request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
+ let websocket = WebSocketTransport(request: request, sendOperationIdentifiers: false, reconnectionInterval: 30000, connectingPayload: map)
+ let splitNetworkTransport = SplitNetworkTransport(
+ httpNetworkTransport: HTTPNetworkTransport(
+ url: endpointURL,
+ session: URLSession(configuration: configuration)
+ ),
+ webSocketNetworkTransport: websocket
+ )
- return ApolloClient(networkTransport: HTTPNetworkTransport(url: endpointURL, configuration: configuration))
+ return ApolloClient(networkTransport: splitNetworkTransport)

Note that we are replacing HTTPNetworkTransport with SplitNetworkTransport and hence all GraphQL queries go through a single websocket connection.


Get Started with GraphQL Now

Hasura Cloud gives you a fully managed, production ready GraphQL API as a service to help you build modern apps faster.
Ready to get started?
Start for free on Hasura Cloud or you could contact our sales team for a detailed walk-through on how Hasura may benefit your business.
Stay in the know
Sign up for full access to our community highlights, new features, and occasional baby animal gifs! Oh, and we have a strict no-spam rule. โœŒ๏ธ