### Declare Krossbow Ktor with CIO engine Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/ktor.md Example of adding both the Krossbow Ktor module and a specific Ktor engine dependency. ```kotlin implementation("org.hildan.krossbow:krossbow-websocket-ktor:{{ git.short_tag }}") implementation("io.ktor:ktor-client-cio:{{ versions.ktor }}") ``` -------------------------------- ### Add Tyrus Dependency for WebSocket Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/migration-guides.md If Tyrus was not explicitly added, you must now add a JSR-356 implementation. This example shows how to add the Tyrus standalone client to `build.gradle.kts`. ```gradle dependencies { implementation("org.glassfish.tyrus.bundles:tyrus-standalone-client-jdk:{{ versions.tyrus }}") } ``` -------------------------------- ### Initialize and use STOMP client Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/getting-started.md Create a client, connect to a URL, and perform basic send and subscribe operations. ```kotlin val client = StompClient(WebSocketClient.builtIn()) // other config can be passed in here val session: StompSession = client.connect(url) // optional login/passcode can be provided here // Send text messages using this convenience function session.sendText(destination = "/some/destination", body = "Basic text message") // Sometimes no message body is necessary session.sendEmptyMsg(destination = "/some/destination") // This subscribe() call triggers a SUBSCRIBE frame // and returns the flow of messages for the subscription val subscription: Flow = session.subscribeText("/some/topic/destination") // Use an appropriate coroutine 'scope' to collect the received frames val collectorJob = scope.launch { subscription.collect { msg -> println("Received: $msg") } } delay(3000) // cancelling the flow collector triggers an UNSUBSCRIBE frame collectorJob.cancel() session.disconnect() ``` -------------------------------- ### Initialize StompClient with KtorWebSocketClient Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/ktor.md Basic instantiation of the StompClient using the default KtorWebSocketClient. ```kotlin val client = StompClient(KtorWebSocketClient()) ``` -------------------------------- ### Initialize binary format STOMP session Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/kx-serialization.md Uses withBinaryConversions to configure a non-JSON format like Protobuf. ```kotlin val session = StompClient(WebSocketClient.builtIn()).connect(url) val jsonStompSession = session.withBinaryConversions(Protobuf.Default, "application/x-protobuf") ``` -------------------------------- ### Initialize StompClient with built-in WebSocket Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/migration-guides.md Use this constructor pattern when migrating from version 3.x to 4.x to explicitly provide the built-in WebSocket client. ```kotlin StompClient(WebSocketClient.default()) ``` -------------------------------- ### Initialize StompClient with SockJS Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/sockjs.md Instantiates a StompClient using the SockJS transport layer. ```kotlin val client = StompClient(SockJSClient()) ``` -------------------------------- ### Configure StompClient with a StompConfig object Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/config.md Create a StompConfig instance separately and pass it to the StompClient constructor. ```kotlin val stompConfig = StompConfig().apply { connectionTimeout = 3.seconds gracefulDisconnect = false } val stompClient = StompClient(WebSocketClient.builtIn(), stompConfig) ``` -------------------------------- ### Create StompClient with OkHttpWebSocketClient Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/okhttp.md Instantiate StompClient using OkHttpWebSocketClient for STOMP messaging over OkHttp WebSockets. ```kotlin val client = StompClient(OkHttpWebSocketClient()) ``` -------------------------------- ### Configure StompClient with a lambda block Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/config.md Use a lambda block during construction to set configuration properties like connection timeout and graceful disconnect. ```kotlin val stompClient = StompClient(WebSocketClient.builtIn()) { connectionTimeout = 3.seconds gracefulDisconnect = false } ``` -------------------------------- ### Initialize StompClient with Adapted Spring Client Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/spring.md Initialize Krossbow's `StompClient` by passing an adapted Spring web socket client. The client must first be converted to a Krossbow `WebSocketClient` using `.asKrossbowWebSocketClient()`. ```kotlin val stompClient = StompClient(StandardWebSocketClient().asKrossbowWebSocketClient()) ``` -------------------------------- ### STOMP Session with Moshi Conversion Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/moshi.md Demonstrates how to create a Moshi instance with Kotlin support and use it to establish a STOMP session. This session allows sending typed objects and subscribing to typed messages, with automatic JSON conversion handled by Moshi. ```kotlin val moshi = Moshi.Builder() .addLast(KotlinJsonAdapterFactory()) .build() StompClient(WebSocketClient.builtIn()).connect(url).withMoshi(moshi).use { session -> session.convertAndSend("/some/destination", Person("Bob", 42)) val messages: Flow = session.subscribe("/some/topic/destination") val firstMessage: MyMessage = messages.first() println("Received: $firstMessage") } ``` -------------------------------- ### Connect to WebSocket without Subprotocols Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/migration-guides.md Use this when you need to manually connect at the STOMP level after establishing a WebSocket connection without any subprotocols. Requires a WebSocket client implementation. ```kotlin val client: WebSocketClient = TODO("get some web socket client implementation") val wsConnection = client.connect(url) // without any subprotocols val stompConfig = StompConfig().apply { // set your config here if needed } val stompSession = wsConnection.stomp(config) ``` -------------------------------- ### Add dependencies for general formats Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/kx-serialization.md Required dependencies for using format-agnostic Krossbow serialization with a specific format like Protobuf. ```kotlin implementation("org.hildan.krossbow:krossbow-stomp-kxserialization:{{ git.short_tag }}") implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:{{ versions.kotlinxSerialization }}") ``` -------------------------------- ### Initialize JSON STOMP session Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/kx-serialization.md Converts a standard StompSession into a StompSessionWithKxSerialization using the JSON format helper. ```kotlin val session = StompClient(WebSocketClient.builtIn()).connect(url) val jsonStompSession = session.withJsonConversions() ``` -------------------------------- ### Configure Krossbow STOMP dependencies Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/getting-started.md Add the core STOMP library and the built-in web socket adapter to your project dependencies. ```kotlin implementation("org.hildan.krossbow:krossbow-stomp-core:{{ git.short_tag }}") implementation("org.hildan.krossbow:krossbow-websocket-builtin:{{ git.short_tag }}") ``` -------------------------------- ### Connect STOMP with Custom WebSocket Headers Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/advanced-features.md Use the `stomp()` extension function on an existing WebSocket session to establish a STOMP connection with custom headers. This is useful when the standard `StompClient.connect()` does not support necessary handshake headers, especially in non-browser environments. ```kotlin val webSocketClient = WebSocketClient.builtIn() // or another web socket client // connect at web socket level with custom headers val wsSession = webSocketClient.connect(url, headers = mapOf("Custom-Header" to "custom-value")) val config = StompConfig().apply { // here you can set up whatever config you would have done in the StompClient { ... } block } // connect at STOMP level on this open web socket, using the above config val stompSession = wsSession.stomp(config) ``` -------------------------------- ### Configure and Adapt Spring WebSocketClient Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/spring.md Customize Spring's `StandardWebSocketClient` with specific configurations like `taskExecutor` and `userProperties` before adapting it to Krossbow for use with `StompClient`. ```kotlin // Pure Spring configuration val springWsClient = StandardWebSocketClient().apply { taskExecutor = SimpleAsyncTaskExecutor("my-websocket-threads") userProperties = mapOf("my-prop" to "someValue") } // Krossbow adapter val stompClient = StompClient(springWsClient.asKrossbowWebSocketClient()) ``` -------------------------------- ### Configure custom Ktor HttpClient Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/ktor.md Pass a pre-configured Ktor HttpClient to the KtorWebSocketClient to customize underlying network behavior. ```kotlin // 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) ``` -------------------------------- ### Customize OkHttpClient for OkHttpWebSocketClient Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/okhttp.md Configure the underlying OkHttpClient for OkHttpWebSocketClient to customize connection behavior like timeouts and ping intervals. This allows using an existing OkHttpClient instance. ```kotlin val okHttpClient = OkHttpClient.Builder() .callTimeout(Duration.ofMinutes(1)) .pingInterval(Duration.ofSeconds(10)) .build() val wsClient = OkHttpWebSocketClient(okHttpClient) val stompClient = StompClient(wsClient) ``` -------------------------------- ### Configure and Adapt Spring SockJS Client Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/spring.md Create a Spring `SockJsClient` with custom transports, including `WebSocketTransport` and `RestTemplateXhrTransport`, then adapt it to Krossbow for use with `StompClient`. ```kotlin // Pure Spring configuration val transports = listOf( WebSocketTransport(StandardWebSocketClient()), RestTemplateXhrTransport(myCustomRestTemplate), ) val springSockJsWsClient = SockJsClient(transports) // Krossbow adapter val stompClient = StompClient(springSockJsWsClient.asKrossbowWebSocketClient()) ``` -------------------------------- ### Send and receive typed messages with JSON Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/kx-serialization.md Demonstrates sending and subscribing to typed data classes using Kotlinx Serialization serializers. ```kotlin @Serializable data class Person(val name: String, val age: Int) @Serializable data class MyMessage(val timestamp: Long, val author: String, val content: String) jsonStompSession.use { s -> s.convertAndSend("/some/destination", Person("Bob", 42), Person.serializer()) // overloads without explicit serializers exist, but should be avoided if you also target JavaScript val messages: Flow = s.subscribe("/some/topic/destination", MyMessage.serializer()) messages.collect { msg -> println("Received message from ${msg.author}: ${msg.content}") } } ``` -------------------------------- ### Manage STOMP session with use block Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/getting-started.md Automatically handle session disconnection using the use extension function. ```kotlin import kotlinx.coroutines.flow.* import org.hildan.krossbow.stomp.* import org.hildan.krossbow.websocket.* import org.hildan.krossbow.websocket.builtin.* val client = StompClient(WebSocketClient.builtIn()) // other config can be passed in here val session: StompSession = client.connect(url) // optional login/passcode can be provided here session.use { s -> s.sendText("/some/destination", "Basic text message") val subscription: Flow = s.subscribeText("/some/topic/destination") // terminal operators that finish early (like first) also trigger UNSUBSCRIBE automatically val firstMessage: String = subscription.first() println("Received: $firstMessage") } // DISCONNECT frame was automatically sent at the end of the use{...} block ``` -------------------------------- ### Configure custom JSON instance Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/kx-serialization.md Passes a custom Json configuration object to the withJsonConversions method. ```kotlin // custom Json configuration val json = Json { encodeDefaults = true ignoreUnknownKeys = true } val session = StompClient(WebSocketClient.builtIn()).connect(url) val jsonStompSession = session.withJsonConversions(json) ``` -------------------------------- ### Declare Gradle dependency Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/sockjs.md Add the required dependency to your build.gradle file to enable SockJS support. ```kotlin implementation("org.hildan.krossbow:krossbow-websocket-sockjs:{{ git.short_tag }}") ``` -------------------------------- ### Use Custom Converter with StompSession Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/custom.md Wrap your `StompSession` with `withTextConversions` using your custom converter. This allows the session to automatically handle conversions for sending and receiving messages of specific types. Ensure your custom converter's MIME type matches the expected content type. ```kotlin StompClient(WebSocketClient.builtIn()).connect(url).withTextConversions(myConverter).use { session -> session.convertAndSend("/some/destination", MyPojo("Custom", 42)) val messages = session.subscribe("/some/topic/destination") val firstMessage: MyMessage = messages.first() println("Received: $firstMessage") } ``` -------------------------------- ### Add dependency for JSON format Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/kx-serialization.md Dependency for the all-in-one JSON module which includes transitive dependencies. ```kotlin implementation("org.hildan.krossbow:krossbow-stomp-kxserialization-json:{{ git.short_tag }}") ``` -------------------------------- ### Use Kotlin Duration API for Timeouts Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/migration-guides.md Replaces millisecond-based timeouts with Kotlin's Duration API. Ensure to import `kotlin.time.Duration.Companion.milliseconds` or `kotlin.time.Duration.Companion.seconds` as needed. ```kotlin val stomp = StompClient { connectionTimeoutMillis = 2000 receiptTimeoutMillis = 5000 disconnectTimeoutMillis = 300 } ``` ```kotlin import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds val stomp = StompClient { connectionTimeout = 2.seconds receiptTimeout = 5.seconds disconnectTimeout = 300.milliseconds } ``` -------------------------------- ### Add Krossbow OkHttp Dependency Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/okhttp.md Declare the Gradle dependency for the krossbow-websocket-okhttp module to use OkHttp as a WebSocket transport. ```gradle implementation("org.hildan.krossbow:krossbow-websocket-okhttp:{{ git.short_tag }}") ``` -------------------------------- ### Adapt Spring WebSocketClient to Krossbow Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/spring.md Use the `.asKrossbowWebSocketClient()` extension to adapt any of Spring's `WebSocketClient` to Krossbow's web socket client interface. This is useful for wrapping Spring's standard JSR-356 client. ```kotlin StandardWebSocketClient().asKrossbowWebSocketClient() ``` -------------------------------- ### Add krossbow-stomp-moshi Dependency Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/moshi.md Include the krossbow-stomp-moshi dependency in your Gradle build file to enable Moshi-based JSON conversions for STOMP. ```gradle implementation("org.hildan.krossbow:krossbow-stomp-moshi:{{ git.short_tag }}") ``` -------------------------------- ### Gradle Dependency for Spring Adapters Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/spring.md Declare the Gradle dependency for the `krossbow-websocket-spring` module to enable the use of Spring adapters. This dependency transitively includes `spring-websocket`. ```gradle implementation("org.hildan.krossbow:krossbow-websocket-spring:{{ git.short_tag }}") ``` -------------------------------- ### Add Krossbow Built-in Dependency Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/builtin.md Include the krossbow-websocket-builtin module in your Gradle project configuration. ```kotlin implementation("org.hildan.krossbow:krossbow-websocket-builtin:{{ git.short_tag }}") ``` -------------------------------- ### Add krossbow-stomp-jackson Dependency Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/jackson.md Include the `krossbow-stomp-jackson` dependency in your Gradle build file to enable STOMP messaging with Jackson JSON conversions. This dependency transitively includes Jackson and its Kotlin module. ```gradle implementation("org.hildan.krossbow:krossbow-stomp-jackson:{{ git.short_tag }}") ``` -------------------------------- ### Add Krossbow Dependency Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/custom.md Include this dependency in your build.gradle(.kts) file to use Krossbow's interfaces and helpers. ```kotlin implementation("org.hildan.krossbow:krossbow-websocket-core:{{ git.short_tag }}") ``` -------------------------------- ### Declare Krossbow Ktor dependency Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/ktor.md Gradle dependency required to use the KtorWebSocketClient. ```kotlin implementation("org.hildan.krossbow:krossbow-websocket-ktor:{{ git.short_tag }}") ``` -------------------------------- ### Gradle Dependency for Tyrus Standalone Client Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/websocket/spring.md Add a dependency on a JSR-356 implementation, such as Tyrus, if you are using Spring's `StandardWebSocketClient`. ```gradle implementation("org.glassfish.tyrus.bundles:tyrus-standalone-client-jdk:{{ versions.tyrus }}") ``` -------------------------------- ### Send and Receive Typed Objects with Jackson Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/jackson.md Use the `withJackson()` extension to convert objects to JSON for sending and deserialize incoming JSON to typed objects. Requires `Person` and `MyMessage` data classes to be defined. ```kotlin StompClient(WebSocketClient.builtIn()).connect(url).withJackson().use { session -> session.convertAndSend("/some/destination", Person("Bob", 42)) val messages: Flow = session.subscribe("/some/topic/destination") val firstMessage: MyMessage = messages.first() println("Received: $firstMessage") } ``` -------------------------------- ### Implement Custom TextMessageConverter Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/custom.md Implement the `TextMessageConverter` interface to define custom object-to-text and text-to-object conversion logic. Specify the MIME type for your converter. This is useful for integrating with custom serialization formats. ```kotlin val myConverter = object : TextMessageConverter { override val mimeType: String = "application/json;charset=utf-8" override fun convertToString(value: T, type: KTypeRef): String { TODO("your own object -> text conversion") } override fun convertFromString(text: String, type: KTypeRef): T { TODO("your own text -> object conversion") } } ``` -------------------------------- ### Add Krossbow STOMP dependency in Gradle Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/artifacts.md Include the desired STOMP artifact in your build.gradle file to enable STOMP protocol support. ```kotlin implementation("org.hildan.krossbow:krossbow-stomp-kxserialization:{{ git.short_tag }}") ``` -------------------------------- ### Use Flow instead of Channel for WebSocket Incoming Frames Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/migration-guides.md The `incomingFrames` channel in WebSocket connections is now a `Flow`. Use `collect` to process incoming frames. ```kotlin val conn = wsClient.connect(url) for (frame in conn.incomingFrames) { // do stuff } ``` ```kotlin val conn = wsClient.connect(url) conn.incomingFrames.collect { // do stuff } ``` -------------------------------- ### Configure Custom ObjectMapper for Jackson Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/stomp/conversions/jackson.md Provide a custom `ObjectMapper` instance to `withJackson()` for fine-grained control over JSON serialization and deserialization behavior, such as disabling specific deserialization features. ```kotlin val customObjectMapper: ObjectMapper = jacksonObjectMapper() .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) .disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES) .enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS) val client = StompClient(WebSocketClient.builtIn()).connect(url) val session = client.withJackson(customObjectMapper) ``` -------------------------------- ### StompSession.use passes session as argument Source: https://github.com/joffrey-bion/krossbow/blob/main/docs/migration-guides.md The `StompSession.use` lambda now receives the session as an argument (`it` or a named parameter) instead of as the receiver (`this`), aligning with `Closeable.use`. ```kotlin StompClient().connect(url).use { sendText("/dest", "message") } ``` ```kotlin StompClient().connect(url).use { it.sendText("/dest", "message") } ``` ```kotlin StompClient().connect(url).use { session -> session.sendText("/dest", "message") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.