Skip to content

Migrate chat sample to new transport API and kotlinx.io #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions samples/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

* api - shared chat API for both client and server
* client - client API implementation via requesting to RSocket with Protobuf serialization.
Works on JVM(TCP/WS), Native(TCP/WS), Js(WS).
Works on JVM/Js/WasmJs/Native over TCP and WS.
Tasks for running clients:
* JVM: `run`
* Native: `runDebugExecutable[TARGET]` / `runReleaseExecutable[TARGET]`
(where `[TARGET]` is one of `LinuxX64`, `MacosArm64` or `MacosX64`)
* NodeJs: `jsNodeRun` / `jsNodeDevelopmentRun` / `jsNodeProductionRun`
* Browser: `jsBrowserRun` / `jsBrowserDevelopmentRun` / `jsBrowserProductionRun`
* JVM: `jvmRun`
* Native: `runDebugExecutable[TARGET]` / `runReleaseExecutable[TARGET]`
(where `[TARGET]` is one of `LinuxX64`, `MacosArm64`, `MacosX64` or `MingwX64`)
* JS: `jsNodeRun` / `jsNodeDevelopmentRun` / `jsNodeProductionRun`
* WasmJs: `wasmJsNodeRun` / `wasmJsNodeDevelopmentRun` / `wasmJsNodeProductionRun`
* server - server API implementation with storage in concurrent map
and exposing it through RSocket with Protobuf serialization.
Can be started on JVM(TCP/WS), Native(TCP/WS), NodeJS(TCP).
Tasks for running servers:
* JVM: `run`
* Native: `runDebugExecutable[TARGET]` / `runReleaseExecutable[TARGET]`
(where `[TARGET]` is one of `LinuxX64`, `MacosArm64` or `MacosX64`)
* NodeJs: `jsNodeRun` / `jsNodeDevelopmentRun` / `jsNodeProductionRun`
* JVM: `jvmRun`
* Native: `runDebugExecutable[TARGET]` / `runReleaseExecutable[TARGET]`
(where `[TARGET]` is one of `LinuxX64`, `MacosArm64`, `MacosX64` or `MingwX64`)
* JS: `jsNodeRun` / `jsNodeDevelopmentRun` / `jsNodeProductionRun`
* WasmJs: `wasmJsNodeRun` / `wasmJsNodeDevelopmentRun` / `wasmJsNodeProductionRun`
10 changes: 8 additions & 2 deletions samples/chat/api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2024 the original author or authors.
* Copyright 2015-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,8 @@
* limitations under the License.
*/

import org.jetbrains.kotlin.gradle.*

plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
Expand All @@ -25,12 +27,16 @@ val kotlinxSerializationVersion: String by rootProject
kotlin {
jvm()
js {
browser()
nodejs()
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
nodejs()
}
linuxX64()
macosX64()
macosArm64()
mingwX64()

sourceSets {
commonMain.dependencies {
Expand Down
10 changes: 5 additions & 5 deletions samples/chat/api/src/commonMain/kotlin/Serialization.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,10 @@

package io.rsocket.kotlin.samples.chat.api

import io.ktor.utils.io.core.*
import io.rsocket.kotlin.*
import io.rsocket.kotlin.metadata.*
import io.rsocket.kotlin.payload.*
import kotlinx.io.*
import kotlinx.serialization.*
import kotlinx.serialization.protobuf.*
import kotlin.jvm.*
Expand All @@ -29,7 +29,7 @@ import kotlin.jvm.*
val ConfiguredProtoBuf = ProtoBuf

@ExperimentalSerializationApi
inline fun <reified T> ProtoBuf.decodeFromPayload(payload: Payload): T = decodeFromByteArray(payload.data.readBytes())
inline fun <reified T> ProtoBuf.decodeFromPayload(payload: Payload): T = decodeFromByteArray(payload.data.readByteArray())

@ExperimentalSerializationApi
@OptIn(ExperimentalMetadataApi::class)
Expand All @@ -55,8 +55,8 @@ inline fun <reified I> ProtoBuf.decoding(payload: Payload, block: (I) -> Unit):
}

@OptIn(ExperimentalMetadataApi::class)
fun Payload(route: String, packet: ByteReadPacket = ByteReadPacket.Empty): Payload = buildPayload {
data(packet)
fun Payload(route: String, data: Buffer = Buffer()): Payload = buildPayload {
data(data)
metadata(RoutingMetadata(route))
}

Expand Down
39 changes: 17 additions & 22 deletions samples/chat/api/src/commonMain/kotlin/Servers.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,29 +21,24 @@ enum class TransportType { TCP, WS }
data class ServerAddress(val port: Int, val type: TransportType)

object Servers {
object JVM {
val TCP = ServerAddress(port = 8001, type = TransportType.TCP)
val WS = ServerAddress(port = 8002, type = TransportType.WS)
}

object JS {
val TCP = ServerAddress(port = 7001, type = TransportType.TCP)
}

object Native {
val TCP = ServerAddress(port = 9001, type = TransportType.TCP)
val WS = ServerAddress(port = 9002, type = TransportType.WS)
}
val JVM = listOf(
ServerAddress(port = 9011, type = TransportType.TCP),
ServerAddress(port = 9012, type = TransportType.WS),
)

val WS = setOf(
JVM.WS,
Native.WS
val JS = listOf(
ServerAddress(port = 9051, type = TransportType.TCP),
ServerAddress(port = 9052, type = TransportType.WS),
)
val TCP = setOf(
JVM.TCP,
JS.TCP,
Native.TCP

val WasmJS = listOf(
ServerAddress(port = 9061, type = TransportType.TCP),
ServerAddress(port = 9062, type = TransportType.WS),
)

val ALL = WS + TCP
val Native = listOf(
ServerAddress(port = 9041, type = TransportType.TCP),
ServerAddress(port = 9042, type = TransportType.WS),
)
val ALL = JVM + JS + WasmJS + Native
}
32 changes: 14 additions & 18 deletions samples/chat/client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2024 the original author or authors.
* Copyright 2015-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,33 +14,37 @@
* limitations under the License.
*/

import org.jetbrains.kotlin.gradle.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*

plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
application
}

val rsocketVersion: String by rootProject
val ktorVersion: String by rootProject

application {
mainClass.set("io.rsocket.kotlin.samples.chat.client.AppKt")
}

kotlin {
jvm {
withJava()
@OptIn(ExperimentalKotlinGradlePluginApi::class)
mainRun {
this.mainClass.set("io.rsocket.kotlin.samples.chat.client.AppKt")
}
}
js {
browser()
nodejs()
binaries.executable()
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
nodejs()
binaries.executable()
}
linuxX64()
macosX64()
macosArm64()
mingwX64()
targets.withType<KotlinNativeTarget>().configureEach {
binaries {
executable {
Expand All @@ -52,18 +56,10 @@ kotlin {
sourceSets {
commonMain.dependencies {
implementation(project(":api"))
implementation("io.rsocket.kotlin:rsocket-transport-ktor-websocket-client:$rsocketVersion")
}
jvmMain.dependencies {
implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion")
implementation("io.ktor:ktor-client-cio:$ktorVersion")
}
nativeMain.dependencies {

implementation("io.rsocket.kotlin:rsocket-transport-ktor-tcp:$rsocketVersion")
implementation("io.rsocket.kotlin:rsocket-transport-ktor-websocket-client:$rsocketVersion")
implementation("io.ktor:ktor-client-cio:$ktorVersion")
}
jsMain.dependencies {
implementation("io.ktor:ktor-client-js:$ktorVersion")
}
}
}
44 changes: 0 additions & 44 deletions samples/chat/client/src/commonMain/kotlin/ApiClient.kt

This file was deleted.

81 changes: 81 additions & 0 deletions samples/chat/client/src/commonMain/kotlin/client.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2015-2025 the original author or authors.
*
* 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://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.rsocket.kotlin.samples.chat.client

import io.ktor.client.engine.cio.*
import io.rsocket.kotlin.*
import io.rsocket.kotlin.core.*
import io.rsocket.kotlin.payload.*
import io.rsocket.kotlin.samples.chat.api.*
import io.rsocket.kotlin.transport.ktor.tcp.*
import io.rsocket.kotlin.transport.ktor.websocket.client.*
import kotlinx.coroutines.*
import kotlinx.serialization.*
import kotlin.coroutines.*

@OptIn(ExperimentalSerializationApi::class)
class ApiClient(rSocket: RSocket) {
private val proto = ConfiguredProtoBuf
val users = UserApiClient(rSocket, proto)
val chats = ChatApiClient(rSocket, proto)
val messages = MessageApiClient(rSocket, proto)
}

suspend fun runClient(
addresses: List<ServerAddress>,
name: String,
target: String,
): Unit = supervisorScope {
addresses.forEach { address ->
launch {
val client = ApiClient(coroutineContext, address, name)
val message = "RSocket is awesome! (from $target)"

val chat = client.chats.all().firstOrNull() ?: client.chats.new("rsocket-kotlin chat")

val sentMessage = client.messages.send(chat.id, message)
println("Send to [$address]: $sentMessage")

client.messages.messages(chat.id, -1).collect {
println("Received from [$address]: $it")
}
}
}
}

private suspend fun ApiClient(
context: CoroutineContext,
address: ServerAddress,
name: String,
): ApiClient {
println("Connecting client to: $address")
val connector = RSocketConnector {
connectionConfig {
setupPayload { buildPayload { data(name) } }
}
}

val target = when (address.type) {
TransportType.TCP -> KtorTcpClientTransport(context).target(host = "127.0.0.1", port = address.port)
TransportType.WS -> KtorWebSocketClientTransport(context) {
httpEngine(CIO)
}.target(host = "127.0.0.1", port = address.port)
}


return ApiClient(connector.connect(target))
}
33 changes: 0 additions & 33 deletions samples/chat/client/src/commonMain/kotlin/usage.kt

This file was deleted.

Loading
Loading