Krossbow with Ktor¶
Krossbow allows you to use Ktor's web socket as transport for STOMP.
Ktor's implementation supports a variety of platforms and is very popular in the Kotlin world, especially in Kotlin multiplatform.
The krossbow-websocket-ktor
module provides the KtorWebSocketClient
, which adapts Ktor 2.3.10's
HttpClient
to Krossbow's web socket interface.
Usage with StompClient¶
To use the KtorWebSocketClient
pass an instance of it when creating your StompClient
:
val client = StompClient(KtorWebSocketClient())
You can customize the actual Ktor HTTP client used behind the scenes by passing it to KtorWebSocketClient
:
// You may configure Ktor HTTP client as you please,
// but make sure at least the websocket feature is installed
val httpClient = HttpClient {
install(WebSockets)
}
val wsClient = KtorWebSocketClient(httpClient)
val stompClient = StompClient(wsClient)
Dependency information¶
You will need to declare the following Gradle dependency to use the KtorWebSocketClient
:
implementation("org.hildan.krossbow:krossbow-websocket-ktor:7.0.0")
Ktor uses pluggable engines to perform the platform-specific
network operations (just like Krossbow uses different web socket implementations).
You need to pick an engine that supports web sockets in order to use Ktor's HttpClient
with web sockets.
Follow Ktor's documentation to find out more about how to use engines.
For instance, if you want to use Ktor's CIO engine with Krossbow, you need to declare the following:
implementation("org.hildan.krossbow:krossbow-websocket-ktor:7.0.0")
implementation("io.ktor:ktor-client-cio:2.3.10")