Ktor client WebSocket plugin - provides WebSocket support for the Ktor HTTP client on multiple platforms including iOS x64
—
The WebSockets plugin provides configuration options for ping intervals, frame size limits, content conversion, and WebSocket extensions.
Install the WebSockets plugin in your HttpClient configuration:
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.websocket.*
val client = HttpClient(CIO) {
install(WebSockets) {
// Configure plugin options
pingInterval = 20_000 // 20 seconds
maxFrameSize = 1024 * 1024 // 1MB
contentConverter = MyWebSocketConverter()
}
}Controls the automatic ping frame interval for connection keep-alive:
var WebSockets.Config.pingInterval: Long-1L (disabled)-1L to disable automatic pings.Usage:
install(WebSockets) {
pingInterval = 30_000 // Send ping every 30 seconds
// or
pingInterval = -1L // Disable automatic pings
}Sets the maximum allowed size for incoming WebSocket frames:
var WebSockets.Config.maxFrameSize: LongInt.MAX_VALUE.toLong()Usage:
install(WebSockets) {
maxFrameSize = 512 * 1024 // 512KB limit
// or
maxFrameSize = Long.MAX_VALUE // No practical limit
}Configures automatic serialization/deserialization for WebSocket messages:
var WebSockets.Config.contentConverter: WebsocketContentConverter?nullsendSerialized() and deserialization via receiveDeserialized().Usage:
import io.ktor.serialization.kotlinx.*
import kotlinx.serialization.json.*
install(WebSockets) {
contentConverter = KotlinxWebsocketSerializationConverter(Json)
}Configure WebSocket protocol extensions:
fun WebSockets.Config.extensions(block: WebSocketExtensionsConfig.() -> Unit)Usage:
install(WebSockets) {
extensions {
// Configure extensions like compression, etc.
// Note: Extension availability depends on the client engine
}
}import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.websocket.*
import io.ktor.serialization.kotlinx.*
import kotlinx.serialization.json.*
val client = HttpClient(CIO) {
install(WebSockets) {
// Send ping every 20 seconds
pingInterval = 20_000
// Limit frames to 1MB
maxFrameSize = 1024 * 1024
// Enable JSON serialization
contentConverter = KotlinxWebsocketSerializationConverter(Json {
prettyPrint = true
isLenient = true
})
// Configure extensions (if supported by engine)
extensions {
// Extension configuration would go here
}
}
}Not all client engines support all WebSocket features:
Check engine capabilities:
object WebSocketCapability : HttpClientEngineCapability<Unit>
object WebSocketExtensionsCapability : HttpClientEngineCapability<Unit>Usage:
val hasWebSocketSupport = client.engine.supportedCapabilities.contains(WebSocketCapability)
val hasExtensionsSupport = client.engine.supportedCapabilities.contains(WebSocketExtensionsCapability)Configuration errors and WebSocket-specific exceptions:
class WebSocketException(message: String, cause: Throwable?) : IllegalStateExceptionCommon configuration issues:
Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-websockets