WebSocket plugin for Ktor HTTP client enabling full-duplex real-time communication
—
WebSocket plugin installation and configuration for Ktor HTTP client, providing control over connection behavior, frame processing, and content serialization.
Install the WebSocket plugin with configuration options for ping intervals, frame size limits, and content conversion.
/**
* Install WebSockets plugin using configuration block
*/
fun HttpClientConfig<*>.WebSockets(config: WebSockets.Config.() -> Unit)
/**
* Main WebSocket plugin class
*/
class WebSockets internal constructor(
val pingIntervalMillis: Long,
val maxFrameSize: Long,
private val extensionsConfig: WebSocketExtensionsConfig,
val contentConverter: WebsocketContentConverter? = null
)
/**
* Public constructors for WebSockets plugin
*/
constructor(
pingIntervalMillis: Long = PINGER_DISABLED,
maxFrameSize: Long = Int.MAX_VALUE.toLong()
)
constructor()Usage Examples:
import io.ktor.client.*
import io.ktor.client.plugins.websocket.*
// Basic installation with default settings
val client = HttpClient {
install(WebSockets)
}
// Installation with custom configuration
val client = HttpClient {
install(WebSockets) {
pingIntervalMillis = 30_000 // 30 seconds
maxFrameSize = 1024 * 1024 // 1MB
}
}Configuration builder for WebSocket plugin settings.
/**
* WebSockets plugin configuration
*/
@KtorDsl
class Config {
/**
* Sets interval of sending ping frames. Use PINGER_DISABLED to disable ping.
*/
var pingIntervalMillis: Long = PINGER_DISABLED
/**
* Sets maximum frame size in bytes
*/
var maxFrameSize: Long = Int.MAX_VALUE.toLong()
/**
* A converter for serialization/deserialization
*/
var contentConverter: WebsocketContentConverter? = null
/**
* Configure WebSocket extensions
*/
fun extensions(block: WebSocketExtensionsConfig.() -> Unit)
}Usage Examples:
// Configure ping and frame size
val client = HttpClient {
install(WebSockets) {
pingIntervalMillis = 20_000
maxFrameSize = 2 * 1024 * 1024 // 2MB
}
}
// Configure with content converter
val client = HttpClient {
install(WebSockets) {
contentConverter = JsonWebsocketContentConverter()
pingIntervalMillis = 15_000
}
}Alternative constructor and configuration using Kotlin Duration API for more readable time specifications.
/**
* Creates WebSocket plugin with Duration-based ping interval
*/
fun WebSockets(
pingInterval: Duration?,
maxFrameSize: Long = Int.MAX_VALUE.toLong()
): WebSockets
/**
* Extension property for getting ping interval as Duration
*/
val WebSockets.pingInterval: Duration?
/**
* Extension property for configuring ping interval using Duration
*/
var WebSockets.Config.pingInterval: Duration?Usage Examples:
import kotlin.time.Duration.Companion.seconds
import kotlin.time.Duration.Companion.minutes
// Using Duration API in configuration
val client = HttpClient {
install(WebSockets) {
pingInterval = 30.seconds
maxFrameSize = 1024 * 1024
}
}
// Direct constructor with Duration
val plugin = WebSockets(
pingInterval = 1.minutes,
maxFrameSize = 512 * 1024
)Plugin installation companion implementing HttpClientPlugin interface.
/**
* WebSockets plugin companion object
*/
companion object Plugin : HttpClientPlugin<Config, WebSockets> {
override val key: AttributeKey<WebSockets>
/**
* Prepare plugin instance from configuration
*/
override fun prepare(block: Config.() -> Unit): WebSockets
/**
* Install plugin in HTTP client
*/
override fun install(plugin: WebSockets, scope: HttpClient)
}Capability objects indicating WebSocket support in HTTP client engines.
/**
* Indicates if a client engine supports WebSockets
*/
data object WebSocketCapability : HttpClientEngineCapability<Unit>
/**
* Indicates if a client engine supports extensions for WebSocket plugin
*/
data object WebSocketExtensionsCapability : HttpClientEngineCapability<Unit>Exception class for WebSocket-related errors.
/**
* Exception thrown during WebSocket operations
*/
class WebSocketException(message: String, cause: Throwable?) : IllegalStateException {
constructor(message: String) : this(message, cause = null)
}/**
* Constant value used to disable ping functionality (value: 0)
*/
const val PINGER_DISABLED: Long = 0Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-websockets-macosarm64