
SocketIO client implementation enables asynchronous socket creation, streamlined thread safety, and simplified binary message handling. Offers logging integration and example usage across multiple platforms.
KMP (pure Kotlin) implementation of SocketIO client.
| Platform | π Buildsπ + π¬Testsπ¬ |
|---|---|
JVM 17 |
π |
JS (Chrome, Node) |
π |
WasmJS (Chrome, Node) |
π |
Android |
π |
iOS |
π |
macOS |
π |
Windows X64 |
π |
Linux X64 |
π |
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")
}
}
}
}val opt = IO.Options()
// opt.trustAllCerts = true
IO.socket("http://localhost:3000", opt) { 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:
emit only accepts String/Boolean/Number/JsonElement/ByteString, other types will be converted to String using toString(), so there is no way to put ByteString in JsonElement.You can open this project in Android Studio, but please setup iOS example project first:
brew install cocoapods xcodegen
# if you have installed them earlier, you need to remove them at first,
# or run brew link --overwrite xcodegen cocoapods
cd example/iosApp
xcodegen
pod installOtherwise you may see error like below:
> Task :example:shared:podInstall FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':example:shared:podInstall'.
> 'pod install' command failed with code 1.
Error message:
Please, check that podfile contains following lines in header:
source 'https://cdn.cocoapods.org'
Please, check that each target depended on shared contains following dependencies:To check coverage details, run ./gradlew :kmp-socketio:jvmTest --info && ./gradlew koverHtmlReport,
then check kmp-socketio/build/reports/kover/html/index.html.
Before running examples, run node kmp-socketio/src/jvmTest/resources/socket-server.js to start the socket-io echo server,
and update the local IP address in example/shared/src/commonMain/kotlin/com/piasy/kmp/socketio/example/Greeting.kt.
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:jsBrowserDevelopmentRunUse Chrome CORS Unblock extension to workaround with CORS error.
./gradlew :example:shared:wasmJsBrowserDevelopmentRun.\gradlew runKmp_socketioDebugExecutableMingwX64./gradlew runKmp_socketioDebugExecutableLinuxX64./gradlew runKmp_socketioDebugExecutableMacosX64Maven central portal credentials and signing configs are set in ~/.gradle/gradle.properties.
# on Linux: need manual release on website
./gradlew clean publishLinuxX64PublicationToMavenCentralRepository --no-configuration-cache
# on Windows: need manual release on website
.\gradlew clean publishMingwX64PublicationToMavenCentralRepository --no-configuration-cache
# on macOS: need manual release on website
./gradlew clean \
publishKotlinMultiplatformPublicationToMavenCentralRepository \
publishAndroidReleasePublicationToMavenCentralRepository \
publishJvmPublicationToMavenCentralRepository \
publishIosArm64PublicationToMavenCentralRepository \
publishIosSimulatorArm64PublicationToMavenCentralRepository \
publishIosX64PublicationToMavenCentralRepository \
publishMacosArm64PublicationToMavenCentralRepository \
publishMacosX64PublicationToMavenCentralRepository \
publishJsPublicationToMavenCentralRepository \
publishWasmJsPublicationToMavenCentralRepository \
--no-configuration-cacheLogin to https://central.sonatype.com/publishing/deployments, and release them manually.
KMP (pure Kotlin) implementation of SocketIO client.
| Platform | π Buildsπ + π¬Testsπ¬ |
|---|---|
JVM 17 |
π |
JS (Chrome, Node) |
π |
WasmJS (Chrome, Node) |
π |
Android |
π |
iOS |
π |
macOS |
π |
Windows X64 |
π |
Linux X64 |
π |
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")
}
}
}
}val opt = IO.Options()
// opt.trustAllCerts = true
IO.socket("http://localhost:3000", opt) { 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:
emit only accepts String/Boolean/Number/JsonElement/ByteString, other types will be converted to String using toString(), so there is no way to put ByteString in JsonElement.You can open this project in Android Studio, but please setup iOS example project first:
brew install cocoapods xcodegen
# if you have installed them earlier, you need to remove them at first,
# or run brew link --overwrite xcodegen cocoapods
cd example/iosApp
xcodegen
pod installOtherwise you may see error like below:
> Task :example:shared:podInstall FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':example:shared:podInstall'.
> 'pod install' command failed with code 1.
Error message:
Please, check that podfile contains following lines in header:
source 'https://cdn.cocoapods.org'
Please, check that each target depended on shared contains following dependencies:To check coverage details, run ./gradlew :kmp-socketio:jvmTest --info && ./gradlew koverHtmlReport,
then check kmp-socketio/build/reports/kover/html/index.html.
Before running examples, run node kmp-socketio/src/jvmTest/resources/socket-server.js to start the socket-io echo server,
and update the local IP address in example/shared/src/commonMain/kotlin/com/piasy/kmp/socketio/example/Greeting.kt.
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:jsBrowserDevelopmentRunUse Chrome CORS Unblock extension to workaround with CORS error.
./gradlew :example:shared:wasmJsBrowserDevelopmentRun.\gradlew runKmp_socketioDebugExecutableMingwX64./gradlew runKmp_socketioDebugExecutableLinuxX64./gradlew runKmp_socketioDebugExecutableMacosX64Maven central portal credentials and signing configs are set in ~/.gradle/gradle.properties.
# on Linux: need manual release on website
./gradlew clean publishLinuxX64PublicationToMavenCentralRepository --no-configuration-cache
# on Windows: need manual release on website
.\gradlew clean publishMingwX64PublicationToMavenCentralRepository --no-configuration-cache
# on macOS: need manual release on website
./gradlew clean \
publishKotlinMultiplatformPublicationToMavenCentralRepository \
publishAndroidReleasePublicationToMavenCentralRepository \
publishJvmPublicationToMavenCentralRepository \
publishIosArm64PublicationToMavenCentralRepository \
publishIosSimulatorArm64PublicationToMavenCentralRepository \
publishIosX64PublicationToMavenCentralRepository \
publishMacosArm64PublicationToMavenCentralRepository \
publishMacosX64PublicationToMavenCentralRepository \
publishJsPublicationToMavenCentralRepository \
publishWasmJsPublicationToMavenCentralRepository \
--no-configuration-cacheLogin to https://central.sonatype.com/publishing/deployments, and release them manually.