-
Notifications
You must be signed in to change notification settings - Fork 40
Description
When opening connections through the ktor client API, it looks like there's no good way to customize the setup metadata for each connection. It can only be set once for the entire HttpClient
.
Motivation
I'm trying to connect to an RSocket endpoint that requires a JWT token as part of the SETUP
frame. In my client, I want to fetch this token before I open the WebSocket:
val client: HttpClient = TODO()
val token = fetchToken()
val rSocket = httpClient.rSocket {
url.takeFrom(myWebSocketUri())
// TODO: Specify connection setup metadata here?
setupPayload = buildPayload {
metadata(json.encodeToString(mapOf("token" to token)))
}
}
emitAll(rSocket.requestStream(buildPayload { /* ... */ }))
However, it doesn't look like there's an option on the rSocket
callback to specify these options.
Desired solution
Since I want to keep my configured HttpClient
around for the entire lifetime of the application instead of re-creating it for every RSocket connection, it would be good to have an option to set the setup payload on the rSocket
method since that is ultimately what's actually opening the connection in the end.
Considered alternatives
I see that I can set the payload when configuring the engine, but it looks like these would be used for every connection?
install(RSocketSupport) {
connector {
connectionConfig {
setupPayload {
buildPayload {
metadata("todo: I want this to be called for every new connection")
}
}
}
}
}
Maybe an easy change is to call setupPayload
again for every connection, but I think being able to customize this when creating the connection would still be better (I need the function creating the setup payload to be suspending, for instance).