Skip to content

Custom setup payload per connection #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
simolus3 opened this issue Apr 29, 2025 · 1 comment
Open

Custom setup payload per connection #311

simolus3 opened this issue Apr 29, 2025 · 1 comment

Comments

@simolus3
Copy link

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).

@simolus3
Copy link
Author

I should add that if the maintainers agree that adding e.g. a connection: RSocketConnectorBuilder.() -> Unit function to the HttpClient.rSocket, I'd be happy to contribute. Just need some guidance on how this would best be integrated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant