# How to update websocket connection http header for existing kotlin apollo client?

**URL:** <https://community.apollographql.com/t/how-to-update-websocket-connection-http-header-for-existing-kotlin-apollo-client/7953>\
**Category:** Client SDKs\
**Tags:** client, kotlin\
**Created:** [November 21, 2024, 4:26pm UTC](https://community.apollographql.com/t/how-to-update-websocket-connection-http-header-for-existing-kotlin-apollo-client/7953 "2024-11-21T16:26:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hlibk](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@hlibk](https://community.apollographql.com/u/hlibk)\
**Post date:** [November 21, 2024, 4:26pm UTC](https://community.apollographql.com/t/how-to-update-websocket-connection-http-header-for-existing-kotlin-apollo-client/7953/1 "2024-11-21T16:26:05Z")

</div>

I have a necessity to attach authorization header to websocket connections (subscriptions) in my Android application. So from time to time auth token becomes expired and needs to be refreshed.  
Currently I create apollo client like this:

```auto
	ApolloClient.Builder()
		.serverUrl(BuildConfig.DATA_BASE_URL)
		.subscriptionNetworkTransport(
			WebSocketNetworkTransport.Builder()
				.serverUrl(BuildConfig.DATA_BASE_URL)
				.addHeader(AUTHORIZATION, "$BEARER $accessToken")
				.build()
		)
		.build()

```

Is there any way to update the token when it should be refreshed?

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [November 21, 2024, 5:05pm UTC](https://community.apollographql.com/t/how-to-update-websocket-connection-http-header-for-existing-kotlin-apollo-client/7953/2 "2024-11-21T17:05:16Z")

</div>

Hi 👋

> Is there any way to update the token when it should be refreshed?

There are but since WebSockets are not part of the specs, there are sadly multiple ways to do so.

The main question is how do you know when the token expires? Do you get a GraphQL error from your server? Or a WebSocket error? Or something else?

---

<div class="post-metadata">

**Author:** ![hlibk](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@hlibk](https://community.apollographql.com/u/hlibk)\
**Post date:** [November 22, 2024, 8:23am UTC](https://community.apollographql.com/t/how-to-update-websocket-connection-http-header-for-existing-kotlin-apollo-client/7953/3 "2024-11-22T08:23:26Z")

</div>

> The main question is how do you know when the token expires? Do you get a GraphQL error from your server? Or a WebSocket error? Or something else?

Yes, if a token expire, our gateway redirects the request to auth page, so I receive 1002 WebSocket protocol error.

So I wonder if it is possible to change the header in, for example, the **reopenWhen** block?

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [November 22, 2024, 8:59am UTC](https://community.apollographql.com/t/how-to-update-websocket-connection-http-header-for-existing-kotlin-apollo-client/7953/4 "2024-11-22T08:59:59Z")

</div>

Sadly there is no such thing yet. You can only update the url and/or the connection\_init params. Do you mind opening an issue? I’ll look into adding this for the [experimental websockets](https://github.com/apollographql/apollo-kotlin/issues/5862).

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [November 22, 2024, 4:24pm UTC](https://community.apollographql.com/t/how-to-update-websocket-connection-http-header-for-existing-kotlin-apollo-client/7953/5 "2024-11-22T16:24:49Z")

</div>

Turns out I lied, the experimental websockets already support this 🎉

I just added some more docs: [Implement ApolloWebSocketClosedException on darwin targets and update docs by martinbonnin · Pull Request #6275 · apollographql/apollo-kotlin · GitHub](https://github.com/apollographql/apollo-kotlin/pull/6275/files)

The tldr; is to use `retryOnErrorInterceptor()` with a custom interceptor:

```kotlin
private object RetryException : Exception()

private class RetryOnErrorInterceptor : ApolloInterceptor {
    override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
      var attempt = 0
      return flow {
        val request = request.newBuilder()
                .addHttpHeader("Authorization", "Bearer ${accessToken()}")
                .build()
    
        emitAll(chain.proceed(request))
      }.onEach {
        when (val exception = it.exception) {
          is ApolloWebSocketClosedException -> {
            if (exception.code == 1002 && exception.reason == "unauthorized") {
              attempt = 0 // retry immediately
              throw RetryException
            }
          }
          is ApolloNetworkException -> {
            // Retry all network exceptions
            throw RetryException
          }
          else -> {
            // Terminate the subscription
          }
        }
      }.retryWhen { cause, _ ->
        cause is RetryException
      }
    }
}

```
