KMP (pure Kotlin) implementation of SocketIO client.
Platform | 🛠Builds🛠 + 🔬Tests🔬 |
---|---|
JVM 17 |
🚀 |
Browser (Chrome) |
🚀 |
Android |
🚀 |
iOS |
🚀 |
macOS |
🚀 |
Windows X64 |
🚀 |
Linux X64 |
🔮 |
About Linux support: Ktor's curl engine doesn't support websockets now, although CIO engine supports websockets, but it doesn't support TLS.
Ref:
You only need to add gradle dependency:
// add common source set dependency
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.piasy:kmp-socketio:$version")
}
}
}
}
IO.socket("http://localhost:3000", IO.Options()) { socket ->
socket.on(Socket.EVENT_CONNECT) { args ->
println("on connect ${args.joinToString()}")
val bin = UnsafeByteStringOperations.wrapUnsafe(byteArrayOf(0x1, 0x3, 0x1, 0x4))
socket.emit("echo", 1, "2", bin, GMTDate())
}.on("echoBack") { args ->
println("on echoBack ${args.joinToString()}")
}
socket.open()
}
Most of the APIs are the same as socket.io-client-java, here are some differences:
- Create socket is asynchronous, to make it's easier to guarantee thread safety.
- Binary messages can't be nested, because
emit
only accepts String/Boolean/Number/JsonElement/ByteString, other types will be converted to String usingtoString()
, so there is no way to put ByteString in JsonElement.
Logger.setLogger(object : LoggerInterface {
override fun debug(tag: String, log: String) {
TODO("Not yet implemented")
}
override fun info(tag: String, log: String) {
TODO("Not yet implemented")
}
override fun error(tag: String, log: String) {
TODO("Not yet implemented")
}
})
Open the project (the repo root dir) in Android studio, and run the example.androidApp target.
cd example/iosApp
pod install
# open iosApp.xcworkspace in Xcode, and run it.
Use Chrome CORS Unblock extension to workaround with CORS error.
./gradlew :example:shared:jsBrowserRun
.\gradlew runKmp_socketioDebugExecutableMingwX64
./gradlew runKmp_socketioDebugExecutableMacosX64
Maven central portal credentials and signing configs are set in ~/.gradle/gradle.properties
.
# on Windows: need manual release on website
.\gradlew clean publishMingwX64PublicationToMavenCentralRepository --no-configuration-cache
# on macOS: need manual release on website
./gradlew clean \
publishKotlinMultiplatformPublicationToMavenCentralRepository \
publishJvmPublicationToMavenCentralRepository \
publishIosArm64PublicationToMavenCentralRepository \
publishIosSimulatorArm64PublicationToMavenCentralRepository \
publishIosX64PublicationToMavenCentralRepository \
publishMacosArm64PublicationToMavenCentralRepository \
publishMacosX64PublicationToMavenCentralRepository \
publishJsPublicationToMavenCentralRepository \
--no-configuration-cache
Login to https://central.sonatype.com/publishing/deployments, and release them manually.