WebSocket
See the project website for documentation and APIs.
WebSocket is a Kotlin Multiplatform library providing RFC 6455 compliant WebSocket client
functionality with permessage-deflate compression (RFC 7692).
- JVM/Android: Built on
AsynchronousSocketChannel (NIO2)
- iOS/macOS/tvOS/watchOS:
NWConnection via Network.framework
- Linux: io_uring or epoll with direct zlib
- Node.js/Browser: Native WebSocket API
Features
- Suspend-based API: All I/O operations are coroutine-friendly suspend functions
- Flow-based messages: Receive messages via
incomingMessages: Flow<WebSocketMessage>
- Compression: permessage-deflate with context takeover and custom window bits
- Zero-copy frame pipeline: Direct buffer I/O with SIMD-optimized masking
- Full RFC 6455 compliance: Passes all 517 Autobahn test cases
Installation

dependencies {
implementation("com.ditchoom:websocket:<latest-version>")
}
Find the latest version on Maven Central.
Quick Example
val options = WebSocketConnectionOptions(
name = "echo.websocket.org",
port = 443,
tls = true,
)
val client = WebSocketClient.allocate(options).connect()
client.write("Hello, WebSocket!")
client.incomingMessages.collect { message ->
when (message) {
is WebSocketMessage.Text -> println("Received: ${message.value}")
WebSocketMessage.Binary -> println()
}
}
client.close()
Compression
Request permessage-deflate compression for reduced bandwidth:
val options = WebSocketConnectionOptions(
name = "example.com",
requestCompression = true,
compressionOptions = CompressionOptions(
clientNoContextTakeover = false,
serverNoContextTakeover = false,
),
)
Platform Support
License
Copyright 2022 DitchOoM
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.