Krossbow STOMP and WebSocket Client
repository·main·Indexed 42 days ago
https://github.com/joffrey-bion/krossbowA Kotlin multiplatform STOMP 1.2 client with a coroutine-based API. It supports multiple WebSocket transport adapters including Ktor, OkHttp, Spring, and SockJS, and provides various serialization options via Kotlinx Serialization, Jackson, and Moshi.
What's inside Krossbow
- Krossbow STOMP Core provides a common code implementation of the STOMP (Simple Text Oriented Messaging Protocol) protocol. It serves as the central module for STOMP functionality within the Krossbow ecosystem.
Overview of Krossbow STOMP client
mainKrossbow is a Kotlin multiplatform STOMP 1.2 client designed with a coroutine-based API. It allows developers to interact with STOMP brokers using Kotlin coroutines across different platforms.
Krossbow is highly extensible through adapters, allowing you to use various underlying WebSocket clients such as:
- OkHttp
- Ktor
- Spring
- SockJS
It also includes a built-in, zero-dependency WebSocket implementation for certain platforms.
Overview of Krossbow STOMP and WebSocket client
mainKrossbow is a Kotlin multiplatform STOMP 1.2 client featuring a coroutine-based API. It is built on a web socket client abstraction and can be used either as a STOMP client or as a multiplatform web socket client without the STOMP protocol.
Key STOMP features include:
- Full STOMP 1.2 specification support (including
ACK/NACKand transactions). - Support for text and binary bodies.
- Receipts (waiting for
RECEIPTframes based on receipt headers). - Heartbeats (keep-alive).
- Custom headers.
- Auto-receipts (automatic addition of
receiptheaders to prevent frame loss). - Built-in body conversions (via Kotlinx Serialization or Jackson).
- Custom body converter hooks.
- Automatic
content-lengthheader for sent frames.
- Full STOMP 1.2 specification support (including
Krossbow Web Socket Ktor Overview
mainKrossbow Web Socket Ktor is a module providing WebSocket support for the Krossbow library using the Ktor engine. For detailed documentation, guides, and API references, visit the official project website.Krossbow Web Socket OkHttp Overview
mainKrossbow Web Socket OkHttp is a module providing WebSocket support for the Krossbow library using the OkHttp engine. For detailed documentation, usage guides, and API references, visit the official project website.Use Flow adapters to bridge listener-based WebSocket APIs
mainThe
krossbow-websocket-coremodule provides helper classes to adapt traditional listener-based WebSocket APIs to Krossbow'sFlow-based API.WebSocketListenerFlowAdapter
Use this adapter when your underlying WebSocket API supports
suspendcallbacks. It handles partial message assembly automatically and provides backpressure support by suspending the callback caller.UnboundedWsListenerFlowAdapter
Use this adapter for APIs that do not support backpressure (such as the JavaScript browser
WebSocketAPI). The callback functions are notsuspendand return immediately, adding new messages to an unbounded queue.Perform a graceful STOMP disconnect
mainA graceful disconnect ensures the server receives all pending frames before the connection is dropped, following the STOMP specification.
- Enabled: If
graceful-disconnectis enabled in your configuration, callingStompSession.disconnect()will first send aDISCONNECTframe with areceiptheader and wait for aRECEIPTframe from the server before closing the connection. - Disabled (Default): If disabled, calling
StompSession.disconnect()immediately closes the underlying web socket connection. In this mode, there is no guarantee that the server has processed all previously sent messages.
- Enabled: If
Use the built-in() factory method for platform-specific WebSockets
mainThekrossbow-websocket-builtinmodule provides abuiltIn()factory method available incommonsource sets. This method returns the appropriateWebSocketClientimplementation for the current platform (JVM, JS, or Darwin) automatically.Using body conversions for Kotlin objects
mainWhile basic usage handles text, Krossbow supports automatic conversion of Kotlin objects to and from STOMP frame bodies. You can configure these conversions using several libraries:
- Kotlinx Serialization: Recommended for multiplatform projects.
- Jackson: For JVM-only projects.
- Moshi: For JVM-only projects.
- Custom conversions: For specialized logic.
Configure STOMP receipts and suspension
mainSTOMP receipts allow a client to confirm that a server has received a specific frame. Krossbow supports this via two mechanisms:
- Auto-receipts: If
auto-receiptis enabled in yourStompConfig, areceiptheader is automatically added to all client frames that support it. When a receipt is present, the method used to send the frame will suspend (wait) until the correspondingRECEIPTframe is received from the server. - Manual receipts: If
auto-receiptis disabled, you can manually provide areceiptheader in the parameters of certain method overloads.
Error Handling:
- If a
receiptheader is present but noRECEIPTframe is received within the configuredreceipt-timeout, aLostReceiptExceptionis thrown. - If no receipt is provided (and
auto-receiptis disabled), the send method returns immediately after the underlying web socket sends the frame and will never throwLostReceiptException.
- Auto-receipts: If
Configure heart beats for connection keep-alive
mainHeart beats act as a keep-alive mechanism to detect if a connection has been lost. You can configure this using the
heartBeatproperty in yourStompClientconfiguration.Key behaviors:
- Negotiation: Heart beat intervals are negotiated with the server during the connection phase. The actual intervals used are defined by the
CONNECTEDframe received from the server and may differ from your local configuration. - Automation:
StompSessionimplementations automatically handle sending and checking heart beats based on the negotiated values. - Error Handling: If expected heart beats are not received within the required timeframe, a
MissingHeartBeatExceptionis thrown, which results in the failure of active subscriptions.
- Negotiation: Heart beat intervals are negotiated with the server during the connection phase. The actual intervals used are defined by the
Migrate from Krossbow 3.x to 4.x: Module changes
mainJSON Conversions
The
withJsonConversionshelper has moved to thekrossbow-stomp-kxserialization-jsonmodule. This module transitively includeskotlinx-serialization-json.Built-in WebSocket Clients
Built-in websocket implementations were moved to
krossbow-websocket-builtinto support pure Kotlin modules.- If you used
WebSocketClient.default(), change your dependency tokrossbow-websocket-builtin. - If you used the
StompClient()constructor without a client argument, add a dependency onkrossbow-websocket-builtinand pass the client explicitly:StompClient(WebSocketClient.builtIn()). (Note:default()was renamed tobuiltIn()in newer versions).
- If you used