Apollo-android: SubscriptionHeartbeat no "pong" message in sealed class OperationClientMessage

Created on 26 Mar 2021  路  6Comments  路  Source: apollographql/apollo-android

I need to implement heartbeat to Apollo client, like server says: {"type":"ka"}, client: {"type":"pong"}
But there is no option in sealed class OperationClientMessage{}.
I cant override it and add option for "pong" response cause its sealed.

sealed class OperationClientMessage {
  @Deprecated("This method is deprecated. Use an OperationMessageSerializer instead.")
  fun toJsonString(): String =
      try {
        val buffer = Buffer()
        ApolloOperationMessageSerializer.writeClientMessage(this, buffer)
        buffer.readUtf8()
      } catch (e: IOException) {
        throw RuntimeException("Failed to serialize to json", e)
      }

  @Suppress("DeprecatedCallableAddReplaceWith")
  @Throws(IOException::class)
  @Deprecated("This method is deprecated. Use an OperationMessageSerializer instead.")
  fun writeToJson(writer: JsonWriter) {
    with(ApolloOperationMessageSerializer) { writeContentsTo(writer) }
  }

  class Init(@JvmField val connectionParams: Map<String, Any?>) : OperationClientMessage() {
    companion object {
      internal const val TYPE = "connection_init"
    }
  }

  class Start(
      @JvmField
      val subscriptionId: String,
      @JvmField
      val subscription: Subscription<*, *, *>,
      @JvmField
      val scalarTypeAdapters: ScalarTypeAdapters,
      @JvmField
      val autoPersistSubscription: Boolean,
      @JvmField
      val sendSubscriptionDocument: Boolean
  ) : OperationClientMessage() {
    companion object {
      internal const val TYPE = "start"
    }
  }

  class Stop(@JvmField val subscriptionId: String) : OperationClientMessage() {
    companion object {
      internal const val TYPE = "stop"
    }
  }

  class Terminate : OperationClientMessage() {
    companion object {
      internal const val TYPE = "connection_terminate"
    }
  }
}

Am i need to send pull request for heartbeat "pong" response to server, or am i missunderstand something and there is easier way to send "pong" to "ka"?
Thanks for answer

Question

All 6 comments

Are you using apollo-server ? If yes, I don't think it requires a pong message.

If you're using another server with a different transport, you will most likely have to implement your own WebSocketSubscriptionTransport.

Yes i'm using apollo-server go-lang, but it requires a pong message to not disconnect a connection.
Now socket flow is :

  1. connect
  2. server send a "ka" message
  3. client dont send "pong"
  4. server disconnected
  5. client catch disconnect error and reconnect

Ah, that's interesting! Looks like something we should add upstream. Do you want to do a pull request?

I just found out that we use https://github.com/99designs/gqlgen in backend - really dont know does socket realization different than in https://github.com/graphql-go/graphql , but in any case would be nice to have option of "pong" response for server with a different transport. For example - flag in Builder. Yes i want to do a pull request

Got it 馃憤 .

I thought you might be using https://www.apollographql.com/docs/apollo-server/ but graphql-go is another story, I'm not sure what the protocol is there.

I'm a bit reluctant to adding more flags/parameters to WebSocketSubscriptionTransport as we can't account for each and every different transport but it would be nice to offer alternate WebSocketSubscriptionTransport that we could package in a separate maven artifact or repo. In that case, maybe something like GraphQLGoSubscriptionTransport?

Okay, got it, i could try GraphQLGoSubscriptionTransport

Was this page helpful?
0 / 5 - 0 ratings