Ktor client WebSockets plugin for Linux x64 platform - enables full-duplex communication between client and server over TCP connection
—
WebSocket plugin installation and configuration with comprehensive control over connection behavior, frame processing, and extension support.
Install the WebSocket plugin with configuration options for ping intervals, frame sizes, and content converters.
/**
* Install WebSocket plugin with configuration
* @param config Configuration block for WebSocket settings
*/
fun HttpClientConfig<*>.WebSockets(config: WebSockets.Config.() -> Unit)Usage Examples:
val client = HttpClient {
install(WebSockets) {
pingInterval = 30.seconds
maxFrameSize = 1024 * 1024 // 1MB
contentConverter = GsonWebsocketContentConverter()
}
}Main plugin class containing configuration and implementation logic.
/**
* Client WebSocket plugin providing WebSocket support for Ktor HTTP client
* @param pingIntervalMillis Interval between ping messages in milliseconds
* @param maxFrameSize Maximum size of a single WebSocket frame
* @param contentConverter Optional converter for serialization/deserialization
*/
class WebSockets internal constructor(
val pingIntervalMillis: Long,
val maxFrameSize: Long,
private val extensionsConfig: WebSocketExtensionsConfig,
val contentConverter: WebsocketContentConverter? = null
) : HttpClientPlugin<WebSockets.Config, WebSockets> {
constructor(
pingIntervalMillis: Long = PINGER_DISABLED,
maxFrameSize: Long = Int.MAX_VALUE.toLong()
)
constructor()
companion object Plugin : HttpClientPlugin<Config, WebSockets> {
override val key: AttributeKey<WebSockets>
override fun prepare(block: Config.() -> Unit): WebSockets
override fun install(plugin: WebSockets, scope: HttpClient)
}
}Configuration class for customizing WebSocket behavior.
/**
* Configuration class for WebSocket plugin
*/
@KtorDsl
class Config {
/** Interval of sending ping frames in milliseconds. Use PINGER_DISABLED to disable ping */
var pingIntervalMillis: Long
/** Maximum frame size in bytes */
var maxFrameSize: Long
/** Content converter for serialization/deserialization */
var contentConverter: WebsocketContentConverter?
/** Ping interval as Duration. Use null to disable ping */
var pingInterval: Duration?
/**
* Configure WebSocket extensions
* @param block Configuration block for extensions
*/
fun extensions(block: WebSocketExtensionsConfig.() -> Unit)
}Usage Examples:
install(WebSockets) {
// Set ping interval using Duration
pingInterval = 45.seconds
// Set maximum frame size (1MB)
maxFrameSize = 1024 * 1024
// Install content converter
contentConverter = KotlinxSerializationWebsocketContentConverter()
// Configure extensions
extensions {
install(WebSocketDeflateExtension) {
compressionLevel = Deflater.DEFAULT_COMPRESSION
}
}
}Alternative constructor using Kotlin Duration API for more convenient configuration.
/**
* Create WebSocket plugin with Duration-based ping interval
* @param pingInterval Interval between ping messages, null to disable
* @param maxFrameSize Maximum size of a single WebSocket frame
*/
fun WebSockets(
pingInterval: Duration?,
maxFrameSize: Long = Int.MAX_VALUE.toLong()
): WebSocketsUsage Examples:
val client = HttpClient {
install(WebSockets(
pingInterval = 30.seconds,
maxFrameSize = 512 * 1024
))
}Convenient extension properties for working with Duration-based configuration.
/**
* Get ping interval as Duration, null if disabled
*/
val WebSockets.pingInterval: Duration?
/**
* Set/get ping interval as Duration for configuration
*/
var WebSockets.Config.pingInterval: Duration?Engine capability markers for WebSocket support detection.
/**
* 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>/** Constant indicating disabled ping (0L) */
const val PINGER_DISABLED: Long = 0Lval client = HttpClient {
install(WebSockets)
}val client = HttpClient {
install(WebSockets) {
// Enable ping every 30 seconds
pingInterval = 30.seconds
// Set 1MB frame size limit
maxFrameSize = 1024 * 1024
// Add JSON serialization support
contentConverter = GsonWebsocketContentConverter()
// Configure compression extension
extensions {
install(WebSocketDeflateExtension) {
compressionLevel = Deflater.BEST_COMPRESSION
serverNoContextTakeover = false
clientNoContextTakeover = false
}
}
}
}val client = HttpClient {
install(WebSockets) {
// 45 second ping interval
pingIntervalMillis = 45000
// 512KB frame size limit
maxFrameSize = 512 * 1024
}
}Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-websockets-linuxx64