All Projects → rsocket → Rsocket Kotlin

rsocket / Rsocket Kotlin

Licence: apache-2.0
RSocket Kotlin multi-platform implementation

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Rsocket Kotlin

Swoole Src
🚀 Coroutine-based concurrency library for PHP
Stars: ✭ 17,175 (+6608.98%)
Mutual labels:  async, coroutines, websocket
Laravel S
LaravelS is an out-of-the-box adapter between Swoole and Laravel/Lumen.
Stars: ✭ 3,479 (+1258.98%)
Mutual labels:  async, websocket, tcp
Tina
Tina is a teeny tiny, header only, coroutine and job library.
Stars: ✭ 125 (-51.17%)
Mutual labels:  async, coroutines
Mioco
[no longer maintained] Scalable, coroutine-based, fibers/green-threads for Rust. (aka MIO COroutines).
Stars: ✭ 125 (-51.17%)
Mutual labels:  async, coroutines
Haslett
A lightweight WebSocket library for ClojureScript
Stars: ✭ 147 (-42.58%)
Mutual labels:  async, websocket
Coroutines
A simple system for running nested coroutines in C#.
Stars: ✭ 100 (-60.94%)
Mutual labels:  async, coroutines
Ws Machine
WS-Machine is a websocket finite state machine for client websocket connections (Go)
Stars: ✭ 110 (-57.03%)
Mutual labels:  async, websocket
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (-44.14%)
Mutual labels:  async, coroutines
Handle Path Oz
Android Library to handle multiple Uri's(paths) received through Intents.
Stars: ✭ 36 (-85.94%)
Mutual labels:  async, coroutines
Async Tungstenite
Async binding for Tungstenite, the Lightweight stream-based WebSocket implementation
Stars: ✭ 207 (-19.14%)
Mutual labels:  async, websocket
Fooproxy
稳健高效的评分制-针对性- IP代理池 + API服务,可以自己插入采集器进行代理IP的爬取,针对你的爬虫的一个或多个目标网站分别生成有效的IP代理数据库,支持MongoDB 4.0 使用 Python3.7(Scored IP proxy pool ,customise proxy data crawler can be added anytime)
Stars: ✭ 195 (-23.83%)
Mutual labels:  async, coroutines
Vertx Lang Kotlin
Vert.x for Kotlin
Stars: ✭ 215 (-16.02%)
Mutual labels:  async, coroutines
Kotlinx.coroutines
Library support for Kotlin coroutines
Stars: ✭ 10,194 (+3882.03%)
Mutual labels:  async, coroutines
Tascalate Async Await
Async / Await asynchronous programming model for Java similar to the functionality available in C# 5. The implementation is based on continuations for Java (see my other projects).
Stars: ✭ 60 (-76.56%)
Mutual labels:  async, coroutines
Socket
Non-blocking socket and TLS functionality for PHP based on Amp.
Stars: ✭ 122 (-52.34%)
Mutual labels:  async, tcp
Channelstream
Channelstream is a websocket communication server for web applications
Stars: ✭ 52 (-79.69%)
Mutual labels:  async, websocket
Cppcoro
A library of C++ coroutine abstractions for the coroutines TS
Stars: ✭ 2,118 (+727.34%)
Mutual labels:  async, coroutines
React Coroutine
Make your async components compact and descriptive by leveraging the power of the language features
Stars: ✭ 246 (-3.91%)
Mutual labels:  async, coroutines
May
rust stackful coroutine library
Stars: ✭ 909 (+255.08%)
Mutual labels:  async, coroutines
Ktx
LibKTX: Kotlin extensions for LibGDX games and applications
Stars: ✭ 913 (+256.64%)
Mutual labels:  async, coroutines

rsocket-kotlin

RSocket Kotlin multi-platform implementation based on kotlinx.coroutines.

RSocket is a binary protocol for use on byte stream transports such as TCP, WebSockets and Aeron.

It enables the following symmetric interaction models via async message passing over a single connection:

  • request/response (stream of 1)
  • request/stream (finite stream of many)
  • fire-and-forget (no response)
  • event subscription (infinite stream of many)

Learn more at http://rsocket.io

Supported platforms and transports :

Transports are implemented based on ktor to ensure Kotlin multiplatform. So it depends on ktor engines for available transports and platforms (JVM, JS, Native):

  • JVM - TCP and WebSocket for both client and server
  • JS - WebSocket client only
  • Native - TCP client only (linux x64, macos, ios, watchos, tvos)

Interactions

RSocket interface contains 5 methods:

  • Fire and Forget:

    suspend fun fireAndForget(payload: Payload)

  • Request-Response:

    suspend requestResponse(payload: Payload): Payload

  • Request-Stream:

    fun requestStream(payload: Payload): Flow<Payload>

  • Request-Channel:

    fun requestChannel(initPayload: Payload, payloads: Flow<Payload>): Flow<Payload>

  • Metadata-Push:

    suspend fun metadataPush(metadata: ByteReadPacket)

Using in your projects

Make sure, that you use Kotlin 1.4.X.

Gradle:

repositories {
    jcenter()
}
dependencies {
    implementation 'io.rsocket.kotlin:rsocket-core:0.12.0'
    implementation 'io.rsocket.kotlin:rsocket-transport-ktor:0.12.0'

//  client feature for ktor
//    implementation 'io.rsocket.kotlin:rsocket-transport-ktor-client:0.12.0'

//  server feature for ktor 
//    implementation 'io.rsocket.kotlin:rsocket-transport-ktor-server:0.12.0' 

//  one of ktor engines to work with websockets
//  client engines
//    implementation 'io.ktor:ktor-client-js:1.4.3' //js
//    implementation 'io.ktor:ktor-client-cio:1.4.3' //jvm
//    implementation 'io.ktor:ktor-client-okhttp:1.4.3' //jvm

//  server engines (jvm only)
//    implementation 'io.ktor:ktor-server-cio:1.4.3'
//    implementation 'io.ktor:ktor-server-netty:1.4.3'
//    implementation 'io.ktor:ktor-server-jetty:1.4.3'
//    implementation 'io.ktor:ktor-server-tomcat:1.4.3'
}

Gradle Kotlin DSL:

repositories {
    jcenter()
}
dependencies {
    implementation("io.rsocket.kotlin:rsocket-core:0.12.0")
    implementation("io.rsocket.kotlin:rsocket-transport-ktor:0.12.0")

//  client feature for ktor
//    implementation("io.rsocket.kotlin:rsocket-transport-ktor-client:0.12.0")

//  server feature for ktor 
//    implementation("io.rsocket.kotlin:rsocket-transport-ktor-server:0.12.0") 

//  one of ktor engines to work with websockets
//  client engines
//    implementation("io.ktor:ktor-client-js:1.4.3") //js
//    implementation("io.ktor:ktor-client-cio:1.4.3") //jvm
//    implementation("io.ktor:ktor-client-okhttp:1.4.3") //jvm

//  server engines (jvm only)
//    implementation("io.ktor:ktor-server-cio:1.4.3")
//    implementation("io.ktor:ktor-server-netty:1.4.3")
//    implementation("io.ktor:ktor-server-jetty:1.4.3")
//    implementation("io.ktor:ktor-server-tomcat:1.4.3")
}

Usage

Client WebSocket with CIO ktor engine

//create ktor client
val client = HttpClient(CIO) {
    install(WebSockets)
    install(RSocketSupport) {
        connector = RSocketConnector {
            //configure rSocket connector (all values have defaults)
            connectionConfig {
                keepAlive = KeepAlive(
                    interval = 30.seconds,
                    maxLifetime = 2.minutes 
                )

                //payload for setup frame
                setupPayload { buildPayload { data("hello world") } }

                //mime types
                payloadMimeType = PayloadMimeType(
                    data = "application/json",
                    metadata = "application/json"
                )
            }
            
            //optional acceptor for server requests
            acceptor {
                RSocketRequestHandler {
                    requestResponse { it } //echo request payload
                }
            }
        }
    }   
}

//connect to some url
val rSocket: RSocket = client.rSocket("wss://rsocket-demo.herokuapp.com/rsocket")

//request stream
val stream: Flow<Payload> = rSocket.requestStream(Payload.Empty)

//take 5 values and print response
stream.take(5).collect { payload: Payload ->
    println(payload.data.readText())
}

Server WebSocket with CIO ktor engine

//create ktor server
embeddedServer(CIO) {
    install(RSocketSupport) {
        //configure rSocket server (all values have defaults)
        server = RSocketServer {
            //install interceptors
            interceptors {
                forConnection(::SomeConnectionInterceptor)
            }
        }
    }
    //configure routing
    routing {
        //configure route `url:port/rsocket`
        rSocket("rsocket") {
            RSocketRequestHandler {
                //handler for request/response
                requestResponse { request: Payload ->
                    //... some work here
                    delay(500) // work emulation
                    buildPayload {
                        data("data")
                        metadata("metadata")
                    }
                }
                //handler for request/stream      
                requestStream { request: Payload ->
                    flow {
                        repeat(1000) { i ->
                            emit(buildPayload { data("data: $i") })
                        }
                    }
                }
            }
        }
    }
}.start(true)

More examples:

Reactive Streams Semantics

From RSocket protocol:

Reactive Streams semantics are used for flow control of Streams, Subscriptions, and Channels. 
This is a credit-based model where the Requester grants the Responder credit for the number of PAYLOADs it can send. 
It is sometimes referred to as "request-n" or "request(n)".

kotlinx.coroutines doesn't truly support request(n) semantic, but it has flexible CoroutineContext which can be used to achieve something similar. rsocket-kotlin contains RequestStrategy coroutine context element, which defines, strategy for sending of requestN frames.

Example:

//assume we have client
val client: RSocket = TODO()

//and stream
val stream: Flow<Payload> = client.requestStream(Payload("data"))

//now we can use `flowOn` to add request strategy to context of flow
//here we use prefetch strategy which will send requestN for 10 elements, when, there is 5 elements left to collect
//so on call `collect`, requestStream frame with requestN will be sent, and then, after 5 elements will be collected
//new requestN with 5 will be sent, so collect will be smooth 
stream.flowOn(PrefetchStrategy(requestSize = 10, requestOn = 5)).collect { payload: Payload ->
    println(payload.data.readText())
}

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

LICENSE

Copyright 2015-2020 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].