Subscriptions in Apollo Kotlin
Subscriptions are long-lived GraphQL read operations that can update their response over time, enabling clients to receive new data as it becomes available.
The GraphQL spec does not specify a particular protocol to use for subscription operations. Apollo Kotlin supports the following protocols:
WebSocket, using one of the following subprotocols:
subscriptions-transport-ws (⚠️ not actively maintained!)
appsync (also uses
graphql-ws
as Sec-WebSocket-Protocol)
HTTP, using chunked multipart responses
You must use whichever transport is supported by your GraphQL endpoint.
You define a subscription in your app just like you define a query, except you use the subscription
keyword. Here's an example subscription for getting the latest value of a number whenever that number is incremented:
1subscription NumberIncremented {
2 numberIncremented
3}
Unlike with queries and mutations, a subscription operation can include only one field of the Subscription
type. To subscribe to multiple fields, you create multiple subscription operations.
Configuring WebSocket subscriptions
By default, Apollo Kotlin uses the
subscriptions-transport-ws
protocol for subscriptions via theSubscriptionWsProtocol
class. This protocol is no longer actively maintained. It remains the default for backward compatibility purposes.A future version of Apollo Kotlin will change the default to the newer
graphql-ws
protocol andGraphQLWsProtocol
class. If your server already usesgraphql-ws
, make sure to set yourWsProtocol
toGraphQLWsProtocol
.
To use subscriptions over WebSocket, use WebSocketNetworkTransport
:
1val apolloClient = ApolloClient.Builder()
2 .subscriptionNetworkTransport(
3 WebSocketNetworkTransport.Builder()
4 .serverUrl("https://example.com/graphql")
5 .build()
6 )
7 .build()
https://
(or http://
) and wss://
(or ws://
) protocols. Internally, wss://
is renamed to https://
and which one you use does not matter.Customizing your WebSocket protocol
By default, Apollo Kotlin uses subscriptions-transport-ws for backward compatibility purposes, but it supports all the following WebSocket subprotocols:
subscriptions-transport-ws (⚠️ not actively maintained!)
appsync (also uses
graphql-ws
as Sec-WebSocket-Protocol)
To customize your protocol, use the WsProtocol
interface. Apollo Kotlin comes with built-in support for the subprotocols above:
Subprotocol | Class |
---|---|
subscriptions-transport-ws | SubscriptionWsProtocol (default) |
graphql-ws | GraphQLWsProtocol |
appsync | AppSyncWsProtocol |
For example, you can configure a graphql-ws
transport like so:
1val apolloClient = ApolloClient.Builder()
2 .subscriptionNetworkTransport(
3 WebSocketNetworkTransport.Builder()
4 .protocol(GraphQLWsProtocol.Factory())
5 .serverUrl("https://example.com/graphql")
6 .build()
7 )
8 .build()
AWS (Amplitude) AppSync
Configuring AppSync is easy but has some subtle nuances due to AppSync's authorization and domain requirements. In particular, AppSync subscriptions on custom domains must append "/realtime" to their endpoint. By contrast non-custom domains do not append "realtime" to the path and instead incorporate it into the domain.
1private const val REALTIME = "realtime"
2
3private fun wsUrl(url: String, auth: Map<String, String>): String {
4 val realtimeUrl = when {
5 url.contains(REALTIME) -> url
6 url.contains("amazonaws.com") -> {
7 val awsUrlParts = url.split("appsync-api")
8 require(awsUrlParts.size == 2) { "Invalid AWS url: $url" }
9 "${awsUrlParts[0]}appsync-realtime-api${awsUrlParts[1]}"
10 }
11 url.endsWith("/") -> "$url$REALTIME"
12 else -> "$url/$REALTIME"
13 }
14 return AppSyncWsProtocol.buildUrl(
15 baseUrl = realtimeUrl,
16 authorization = auth,
17 )
18}
19
20// assumes host and key variables have already been set
21private fun wsAuth(): Map<String, String> = mapOf("host" to host, "x-api-key" to key)
22
23val apolloClient = ApolloClient.Builder()
24 .subscriptionNetworkTransport(
25 WebSocketNetworkTransport.Builder()
26 // if you know that your URL is correct for realtime then below could be
27 // .serverUrl(AppSyncWsProtocol.buildUrl(url, wsAuth())
28 .serverUrl(wsUrl(url, wsAuth()))
29 .protocol(AppSyncWsProtocol.Factory(connectionPayload = { wsAuth() }))
30 .build(),
31 )
32 .build()
Authentication
Please refer to this section about authentication with WebSocket.
Configuring HTTP subscriptions
To use HTTP for subscriptions, use HttpNetworkTransport
like so:
1val apolloClient = ApolloClient.Builder()
2 .subscriptionNetworkTransport(
3 HttpNetworkTransport.Builder()
4 .serverUrl("https://example.com/graphql")
5 .build()
6 )
7 .build()
This is the only configuration required. HttpNetworkTransport
will use chunked multipart responses for subscription operations and standard POST or GET requests for queries and mutations.
Listening to a subscription
After you configure the NetworkTransport
, use ApolloClient.subscription
to open the connection and listen for changes:
1apolloClient.subscription(TripsBookedSubscription())
2 .toFlow()
3 .collect {
4 println("trips booked: ${it.data?.tripsBooked}")
5 }
Because subscriptions are long-lasting operations, you should call toFlow()
to get a Flow<Response>
instead of a single Response
.
Terminating a subscription
Termination is handled through the coroutine scope. Cancel the coroutine to terminate the subscription.
By default, a single WebSocket is shared between all active subscriptions. When no subscription is active, the WebSocket is closed after a configurable timeout.
Error handling
Like queries, subscriptions support partial responses with GraphQL errors, which are emitted in the Flow
.
Network errors terminate the Flow
, and you need to retry to get new updates. Depending on the situation, retrying might open a new WebSocket or restart the subscription.
See also this section about WebSocket errors handling.