Monika Singh
07/03/2024, 7:09 AMval client = ApolloClient.Builder()
client.networkTransport(
HttpNetworkTransport.Builder()
.serverUrl(serverUrl = endpoint)
.httpEngine(gqlEngine(buildType))
.build(),
)
client.subscriptionNetworkTransport(
WebSocketNetworkTransport.Builder()
.idleTimeoutMillis(20000L)
.protocol(
SubscriptionWsProtocol.Factory(connectionPayload = {
mapOf(
"x-token" to "",
"Authorization" to ""
)
})
)
.serverUrl(serverUrl = endpoint)
.addHeaders(
listOf(
HttpHeader("Accept", "application/json"),
HttpHeader("Content-Type", "application/json"),
HttpHeader("Connection", "keep-alive, Upgrade"),
HttpHeader("Upgrade", "websocket"),
HttpHeader("x-token", "")
)
)
.reopenWhen { _, attempt ->
if (attempt >= 3L) {
false
} else {
delay(attempt * 1000L) // Exponential backoff (1s, 2s, 4s)
true
}
}
.build()
)
public fun <D : Subscription.Data, T> ApolloClient?.subscribe(
subscription: Subscription<D>,
mapper: suspend (data: D?) -> Either<FelixError, T>
): Flow<T> = channelFlow {
try {
this
.subscription(subscription)
.toFlow()
.retryWhen { cause, attempt ->
if (cause is ApolloNetworkException && attempt < 1) {
delay(attempt * 1000L) // Exponential backoff
true
} else {
false
}
}
.map {
mapper(it.data)
}
.catch { e ->
send(
// send exception
)
}.collectLatest {
send(it)
}
} catch (e: Exception) {
send(
// send exception
)
}
}
mbonnin
07/03/2024, 7:19 AMmbonnin
07/03/2024, 7:19 AMMonika Singh
07/08/2024, 8:07 AMmbonnin
07/08/2024, 8:16 PMreopenWhen {}
is the one you want to use for connectivity issues, I’m not sure about the channelFlow {}
and retryWhen {}
in the subscribe
call. Can you reproduce consistently? If you can reproduce the issue with the tutorial, that would help a ton. There’s a subscription where you can trigger events by running this mutation and it will display a toast.mbonnin
07/08/2024, 8:16 PM