1
1
# rsocket-kotlin
2
2
3
3
RSocket Kotlin multi-platform implementation based on
4
- [ kotlinx.coroutines] ( https://github.com/Kotlin/kotlinx.coroutines ) and [ ktor -io] ( https://github.com/ktorio/ktor ) .
4
+ [ kotlinx.coroutines] ( https://github.com/Kotlin/kotlinx.coroutines ) and [ kotlinx -io] ( https://github.com/Kotlin/kotlinx-io ) .
5
5
6
6
RSocket is a binary application protocol providing Reactive Streams semantics for use on byte stream transports such as
7
7
TCP, WebSockets, QUIC and Aeron.
@@ -15,27 +15,13 @@ It enables the following symmetric interaction models via async message passing
15
15
16
16
Learn more at http://rsocket.io
17
17
18
- ## Supported platforms and transports :
18
+ ## Supported platforms and transports:
19
19
20
- Local (in memory) transport is supported on all targets.
21
- Most of other transports are implemented using [ ktor] ( https://github.com/ktorio/ktor ) to ensure Kotlin multiplatform.
22
- So it depends on ` ktor ` client/server engines for available transports and platforms.
23
-
24
- ### Client transports:
25
-
26
- | | TCP | WebSocket |
27
- | -----------------------------| -----------------------------------------| ------------|
28
- | JVM | ✅ via ktor | ✅ via ktor |
29
- | JS | ✅ via nodeJS (not supported in browser) | ✅ via ktor |
30
- | Native<br />(except windows) | ✅ via ktor | ✅ via ktor |
31
-
32
- ### Server transports:
33
-
34
- | | TCP | WebSocket |
35
- | -----------------------------| -----------------------------------------| ------------|
36
- | JVM | ✅ via ktor | ✅ via ktor |
37
- | JS | ✅ via nodeJS (not supported in browser) | ❌ |
38
- | Native<br />(except windows) | ✅ via ktor | ✅ via ktor |
20
+ Local (in memory) transport is supported for all targets.
21
+ Starting from [ Ktor 3.1 release] ( https://blog.jetbrains.com/kotlin/2025/02/ktor-3-1-0-release/ ) ,
22
+ all ktor-client, ktor-server and ktor-network modules are supported for all targets.
23
+ So all Ktor related transports (TCP and WebSocket) are supported by rsocket-kotlin for all targets.
24
+ Additionally, there is experimental JVM-only support for Netty TCP and QUIC transpots.
39
25
40
26
## Using in your projects
41
27
@@ -49,18 +35,18 @@ repositories {
49
35
50
36
### Ktor plugins
51
37
52
- rsocket-kotlin provides [ client] ( https://ktor.io/docs/http- client-plugins.html )
53
- and [ server] ( https://ktor.io/docs/plugins.html ) plugins for [ ktor] ( https://ktor.io )
38
+ rsocket-kotlin provides [ client] ( https://ktor.io/docs/client-plugins.html )
39
+ and [ server] ( https://ktor.io/docs/server- plugins.html ) plugins for [ ktor] ( https://ktor.io )
54
40
55
41
Dependencies:
56
42
57
43
``` kotlin
58
44
dependencies {
59
- // for client
60
- implementation(" io.rsocket.kotlin:rsocket- ktor-client:0.16 .0" )
45
+ // for client
46
+ implementation(" io.rsocket.kotlin:ktor-client-rsocket :0.20 .0" )
61
47
62
- // for server
63
- implementation(" io.rsocket.kotlin:rsocket- ktor-server:0.16 .0" )
48
+ // for server
49
+ implementation(" io.rsocket.kotlin:ktor-server-rsocket :0.20 .0" )
64
50
}
65
51
```
66
52
@@ -69,38 +55,24 @@ Example of client plugin usage:
69
55
``` kotlin
70
56
// create ktor client
71
57
val client = HttpClient {
72
- install(WebSockets ) // rsocket requires websockets plugin installed
58
+ install(WebSockets ) // rsocket requires websockets plugin installed
73
59
install(RSocketSupport ) {
74
- // configure rSocket connector (all values have defaults)
60
+ // configure rSocket connector (all values have defaults)
75
61
connector {
76
- maxFragmentSize = 1024
77
-
78
62
connectionConfig {
79
- keepAlive = KeepAlive (
80
- interval = 30 .seconds,
81
- maxLifetime = 2 .minutes
82
- )
83
-
84
- // payload for setup frame
63
+ // payload for setup frame
85
64
setupPayload {
86
65
buildPayload {
87
66
data(""" { "data": "setup" }""" )
88
67
}
89
68
}
90
69
91
- // mime types
70
+ // mime types
92
71
payloadMimeType = PayloadMimeType (
93
72
data = WellKnownMimeType .ApplicationJson ,
94
73
metadata = WellKnownMimeType .MessageRSocketCompositeMetadata
95
74
)
96
75
}
97
-
98
- // optional acceptor for server requests
99
- acceptor {
100
- RSocketRequestHandler {
101
- requestResponse { it } // echo request payload
102
- }
103
- }
104
76
}
105
77
}
106
78
}
@@ -126,37 +98,26 @@ Example of server plugin usage:
126
98
``` kotlin
127
99
// create ktor server
128
100
embeddedServer(CIO ) {
129
- install(WebSockets ) // rsocket requires websockets plugin installed
101
+ install(WebSockets ) // rsocket requires websockets plugin installed
130
102
install(RSocketSupport ) {
131
- // configure rSocket server (all values have defaults)
132
-
133
- server {
134
- maxFragmentSize = 1024
135
-
136
- // install interceptors
137
- interceptors {
138
- forConnection(::SomeConnectionInterceptor )
139
- }
140
- }
103
+ // optionally configure rSocket server
141
104
}
142
- // configure routing
143
105
routing {
144
- // configure route `/rsocket`
145
106
rSocket(" rsocket" ) {
146
107
println (config.setupPayload.data.readString()) // print setup payload data
147
108
148
109
RSocketRequestHandler {
149
- // handler for request/response
110
+ // handler for request/response
150
111
requestResponse { request: Payload ->
151
112
println (request.data.readString()) // print request payload data
152
113
delay(500 ) // work emulation
153
114
buildPayload {
154
115
data(""" { "data": "Server response" }""" )
155
116
}
156
117
}
157
- // handler for request/stream
118
+ // handler for request/stream
158
119
requestStream { request: Payload ->
159
- println (request.data.readString()) // print request payload data
120
+ println (request.data.readString()) // print request payload data
160
121
flow {
161
122
repeat(10 ) { i ->
162
123
emit(
@@ -181,45 +142,49 @@ Dependencies:
181
142
182
143
``` kotlin
183
144
dependencies {
184
- implementation(" io.rsocket.kotlin:rsocket-core:0.16 .0" )
145
+ implementation(" io.rsocket.kotlin:rsocket-core:0.20 .0" )
185
146
186
147
// TCP ktor client/server transport
187
- implementation(" io.rsocket.kotlin:rsocket-transport-ktor-tcp:0.16 .0" )
148
+ implementation(" io.rsocket.kotlin:rsocket-transport-ktor-tcp:0.20 .0" )
188
149
189
150
// WS ktor client transport
190
- implementation(" io.rsocket.kotlin:rsocket-transport-ktor-websocket-client:0.16 .0" )
151
+ implementation(" io.rsocket.kotlin:rsocket-transport-ktor-websocket-client:0.20 .0" )
191
152
192
153
// WS ktor server transport
193
- implementation(" io.rsocket.kotlin:rsocket-transport-ktor-websocket-server:0.16 .0" )
154
+ implementation(" io.rsocket.kotlin:rsocket-transport-ktor-websocket-server:0.20 .0" )
194
155
195
- // TCP nodeJS client/server transport
196
- implementation(" io.rsocket.kotlin:rsocket-transport-nodejs -tcp:0.16 .0" )
156
+ // Netty TCP client/server transport
157
+ implementation(" io.rsocket.kotlin:rsocket-transport-netty -tcp:0.20 .0" )
197
158
}
198
159
```
199
160
200
- Example of usage standalone client transport:
161
+ Example of usage standalone TCP ktor client transport:
201
162
202
163
``` kotlin
203
-
204
- val transport = TcpClientTransport (" 0.0.0.0" , 8080 )
164
+ val parentContext = Job ()
165
+ val target = KtorTcpClientTransport (parentContext) {
166
+ // optional configuration
167
+ }.target(" 127.0.0.1" , 8080 )
205
168
val connector = RSocketConnector {
206
169
// configuration goes here
207
170
}
208
- val rsocket: RSocket = connector.connect(transport )
171
+ val rsocket: RSocket = connector.connect(target )
209
172
// use rsocket to do request
210
173
val response = rsocket.requestResponse(buildPayload { data(""" { "data": "hello world" }""" ) })
211
174
println (response.data.readString())
212
175
```
213
176
214
- Example of usage standalone server transport:
177
+ Example of usage standalone TCP ktor server transport:
215
178
216
179
``` kotlin
217
-
218
- val transport = TcpServerTransport (" 0.0.0.0" , 8080 )
219
- val connector = RSocketServer {
180
+ val parentContext = Job ()
181
+ val target = KtorTcpServerTransport (parentContext) {
182
+ // optional configuration
183
+ }.target(" 127.0.0.1" , 8080 )
184
+ val server = RSocketServer {
220
185
// configuration goes here
221
186
}
222
- val server : TcpServer = server.bind(transport ) {
187
+ val serverInstance = server.startServer(target ) {
223
188
RSocketRequestHandler {
224
189
// handler for request/response
225
190
requestResponse { request: Payload ->
@@ -231,7 +196,7 @@ val server: TcpServer = server.bind(transport) {
231
196
}
232
197
}
233
198
}
234
- server.handlerJob. join() // wait for server to finish
199
+ serverInstance.coroutineContext.job. join() // wait for server to finish
235
200
```
236
201
237
202
### More samples:
0 commit comments