Subscriptions
GraphQL supports subscriptions to allow clients to be immediately updated when the data changes on a server.
The Apollo iOS library supports the use of subscriptions primarily through the use of ApolloWebSocket, an optional additional library that uses popular iOS WebSocket library Starscream under the hood to use WebSockets to connect to your GraphQL server.
Subscriptions are also supported through code generation: Any time your schema declares a subscription field, an operation conforming to GraphQLSubscription will be generated which allows you to pass in any parameters that subscription field takes.
Once those operations are generated, you can use an instance of ApolloClient using a subscription-supporting network transport to subscribe and continue to receive updates about changes until the subscription is cancelled.
Transport types which support subscriptions
There are two different classes which conform to the NetworkTransport protocol within the ApolloWebSocket library:
- WebSocketTransport sends all operations over a web socket.
- SplitNetworkTransport hangs onto both a WebSocketTransport instance and an UploadingNetworkTransport instance (usually HTTPNetworkTransport) in order to create a single network transport that can use http for queries and mutations, and web sockets for subscriptions.
Typically, you'll want to use SplitNetworkTransport, since this allows you to retain the single NetworkTransport setup and avoids any potential issues of using multiple client objects.
Sample subscription-supporting initializer
Here is an example of setting up a singleton similar to the Example Advanced Client Setup, but which uses a SplitNetworkTransport to support both subscriptions and queries:
Example usage of a subscription
Let's say you're using the Sample Star Wars API, and you want to use a view controller with a UITableView to show a list of reviews that will automatically update whenever a new review is added.
You can use the ReviewAddedSubscription to accomplish this:
Each time a review is added, the subscription's closure is called and if the proper data is included, the new data will be displayed immediately.
Note that if you only wanted to be updated reviews for a specific episode, you could specify that episode in the initializer for ReviewAddedSubscription.
Subscriptions and authorization tokens
In a standard HTTP operation, if authentication is necessary an Authorization header is often sent with requests. However, with a web socket, this can't be sent with every payload since a persistent connection is required.
For web sockets, the connectingPayload provides those parameters you would traditionally specify as part of the headers of your request.
Note that this must be set when the WebSocketTransport is created. If you need to update the connectingPayload, you will need to recreate the client using a new webSocketTransport.
Assuming you (or your backend developers) have read the authentication section and subscriptions example / authentication over WebSocket of our backend documentation, you will need to initialize your ApolloClient instance as follows:
Previous:
Local state management
Next:
Swift scripting