I'm trying to slowly migrate an android app to mul...
# apollo-kotlin
s
I'm trying to slowly migrate an android app to multiplatform. It seems that
addHttpInterceptor
doesn't affect WS connections. Is that intentional? Is there an alternative to using an
OkHttpClient
with an interceptor for web sockets?
m
WS is different from HTTP as it's not request/response but more bidirectional communication
Does okhttp has interceptors for Websockets?
s
Yes, adding an interceptor to the okhttp client works for both http and ws.
m
Wait but a WebSocket may return multiple payloads messages
What am I missing?
s
In my case, I need to send the authentication token. Adding the interceptor to the okhttp client works for me.
m
Looks like it's used for the initial handshake 👀
Let me check how to do this on iOS
s
I'm really not a websockets expert. I did plenty of reading and experimenting a year ago to get this working. It's super late here, so I'm a bit useless currently, sorry.
m
No worries at all!
Turns out the WebSocket initial handshake is actually a HTTP request
I guess this is why the OkHttp interceptor is working for WebSockets
today i learned 1
s
Well, that's good news, I think.
m
Making a multiplatform version of this is going to be challenging though as the APIs are wildly different on iOS and Android
It'd have to be a lambda for the token to be updated on reconnect
Out of curiosity, is there any chance you could send your token in the initial "connection_init" message?
s
That would work for me. I guess you can't just use the http interceptor in there.
Pretty sure I could.
m
I guess you can't just use the http interceptor in there.
Not really.
HttpInterceptor
uses a different API that iOS/OkHttp don't understand
s
It's a hasura server, so if they accept it, certainly. Any idea how I'd do that?
m
Pretty sure I could.
If you can, that's the easiest route,
SubscriptionWsProtocol
has a
connectionPayload
parameter
Something like
Copy code
val apolloClient = ApolloClient.Builder()
        .httpServerUrl("<http://localhost:8080/graphql>")
        .webSocketServerUrl("<http://localhost:8080/subscriptions>")
        .wsProtocol(
            SubscriptionWsProtocol.Factory(
                connectionPayload = {
                  mapOf("authorization" to myToken)
                }
            )
        )
According to this page , hasura is expecting headers though, not a connection payload
s
Give me one minute and I'll test it.
👍 1
m
Exposing a function to customize headers wouldn't harm in all cases. Let me know if you want to open a pull request. If not, I'll try to get this in next version
s
Finished testing. No luck.
👀 1
I guess there's no rush on it. I'm going to be traveling the next few days. I might find some time in there or when I get back. If you don't mind the wait, I'll try to make a pull request.
m
Nice ! Not sure when we'll do the next version, maybe end of next week or so. Sounds like something that'd be a nice addition