WebSocket plugin for Ktor HTTP client enabling full-duplex real-time communication
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Ktor Client WebSockets provides comprehensive WebSocket client support for the Ktor HTTP client framework. It enables full-duplex real-time communication over a single TCP connection, supporting both text and binary message types with automatic connection management, configurable frame sizes and ping intervals, and seamless integration with Ktor's multiplatform architecture.
implementation("io.ktor:ktor-client-websockets:3.2.0")import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.websocket.*
import io.ktor.websocket.*import io.ktor.client.*
import io.ktor.client.plugins.websocket.*
import io.ktor.websocket.*
// Install WebSocket plugin
val client = HttpClient {
install(WebSockets) {
pingIntervalMillis = 20_000 // 20 seconds
maxFrameSize = Long.MAX_VALUE
}
}
// Basic WebSocket connection
client.webSocket("ws://localhost:8080/websocket") {
// Send text message
send("Hello, WebSocket!")
// Receive and handle incoming frames
for (frame in incoming) {
when (frame) {
is Frame.Text -> {
val receivedText = frame.readText()
println("Received: $receivedText")
}
is Frame.Binary -> {
val receivedBytes = frame.readBytes()
println("Received binary data: ${receivedBytes.size} bytes")
}
is Frame.Close -> {
println("Connection closed")
break
}
else -> {}
}
}
}Ktor Client WebSockets is built around several key components:
Core WebSocket plugin installation and configuration, including ping intervals, frame size limits, and content serialization.
class WebSockets(
val pingIntervalMillis: Long,
val maxFrameSize: Long,
val contentConverter: WebsocketContentConverter? = null
)
class Config {
var pingIntervalMillis: Long
var maxFrameSize: Long
var contentConverter: WebsocketContentConverter?
fun extensions(block: WebSocketExtensionsConfig.() -> Unit)
}Functions for establishing and managing WebSocket connections with support for various URL formats and request configurations.
suspend fun HttpClient.webSocket(
request: HttpRequestBuilder.() -> Unit,
block: suspend DefaultClientWebSocketSession.() -> Unit
)
suspend fun HttpClient.webSocketSession(
block: HttpRequestBuilder.() -> Unit
): DefaultClientWebSocketSession
suspend fun HttpClient.wss(
request: HttpRequestBuilder.() -> Unit,
block: suspend DefaultClientWebSocketSession.() -> Unit
)Client-specific WebSocket session interfaces providing access to HTTP call context and message serialization capabilities.
interface ClientWebSocketSession : WebSocketSession {
val call: HttpClientCall
}
class DefaultClientWebSocketSession(
override val call: HttpClientCall,
delegate: DefaultWebSocketSession
) : ClientWebSocketSession, DefaultWebSocketSession by delegate
suspend fun <T> DefaultClientWebSocketSession.sendSerialized(data: T)
suspend fun <T> DefaultClientWebSocketSession.receiveDeserialized(): Tdata object WebSocketCapability : HttpClientEngineCapability<Unit>
data object WebSocketExtensionsCapability : HttpClientEngineCapability<Unit>
class WebSocketException(message: String, cause: Throwable?) : IllegalStateException
val DefaultClientWebSocketSession.converter: WebsocketContentConverter?