|
1 | | -# RSOCKET-ANDROID |
| 1 | +# RSOCKET-KOTLIN |
| 2 | +<a href='https://travis-ci.org/rsocket/rsocket-kotlin/builds'><img src='https://travis-ci.org/rsocket/rsocket-kotlin.svg?branch=master'></a> [](https://gitter.im/ReactiveSocket/reactivesocket-java) |
2 | 3 |
|
3 | | -This is an implementation of [RSocket](http://rsocket.io/) - binary application protocol bringing [Reactive-Streams](http://www.reactive-streams.org/) semantics |
4 | | -for network communications. |
5 | | -`Kotlin` & `RxJava2` backport of [RSocket-java](https://github.com/rsocket/rsocket-java) intended for pre java8 runtimes. |
6 | | -Relies on `OkHttp` as WebSockets transport. Target platform is Android (tested on API level 4.4+) |
| 4 | +R(eactive)Socket: [Reactive Streams](http://www.reactive-streams.org/) over network boundary (tcp, websockets, etc) using Kotlin/Rxjava |
| 5 | + |
| 6 | +RSocket is binary application protocol which models all communication as multiplexed streams of messages over a single network connection, and never synchronously blocks while waiting for a response. |
| 7 | + |
| 8 | +It enables following symmetric interaction models: |
| 9 | + |
| 10 | +* fire-and-forget (no response) |
| 11 | +* request/response (stream of 1) |
| 12 | +* request/stream (finite/infinite stream of many) |
| 13 | +* channel (bi-directional streams) |
| 14 | + |
| 15 | +Also, metadata can be associated with stream or RSocket itself |
7 | 16 |
|
8 | | -Supports 4 interaction models: fire-and-forget, request-response, request-stream, request-channel. |
9 | | - |
10 | 17 | ## Build and Binaries |
11 | 18 |
|
12 | | -<a href='https://travis-ci.org/rsocket/rsocket-android/builds'><img src='https://travis-ci.org/rsocket/rsocket-android.svg?branch=master'></a> |
13 | | - |
14 | | - |
15 | | - The project is not released yet, snapshots are available on Bintray |
| 19 | + Snapshots are available on Bintray |
16 | 20 | ```groovy |
17 | 21 | repositories { |
18 | 22 | maven { url 'https://oss.jfrog.org/libs-snapshot' } |
19 | 23 | } |
20 | | -``` |
21 | | - |
| 24 | +``` |
22 | 25 |
|
23 | 26 | ```groovy |
24 | | - dependencies { |
25 | | - compile 'io.rsocket:rsocket-android-core:0.9-SNAPSHOT' |
26 | | - compile 'io.rsocket:rsocket-transport-okhttp:0.9-SNAPSHOT' |
27 | | - } |
| 27 | + dependencies { |
| 28 | + compile 'io.rsocket.android:rsocket-core:0.9-SNAPSHOT' |
| 29 | + } |
| 30 | +``` |
| 31 | +### Transports |
| 32 | +`Netty` based Websockets and TCP transport (`Client` and `Server`) |
| 33 | +`OkHttp` based Websockets transport (`Client` only) |
| 34 | +```groovy |
| 35 | + dependencies { |
| 36 | + compile 'io.rsocket.android:rsocket-transport-netty:0.9-SNAPSHOT' |
| 37 | + compile 'io.rsocket.android:rsocket-transport-okhttp:0.9-SNAPSHOT' |
| 38 | + } |
| 39 | +``` |
| 40 | +### Usage |
| 41 | +Each side of connection (Client and Server) has `Requester RSocket` for making requests to peer, and `Responder RSocket` to handle requests from peer. |
| 42 | + |
| 43 | +Messages for all interactions are represented as `Payload` of binary (`NIO ByteBuffer`) data and metadata. |
| 44 | + |
| 45 | +UTF-8 `text` payloads can be constructed as follows |
| 46 | +```kotlin |
| 47 | +val request = PayloadImpl.textPayload("data", "metadata") |
| 48 | +``` |
| 49 | +Stream Metadata is optional |
| 50 | +```kotlin |
| 51 | +val request = PayloadImpl.textPayload("data") |
28 | 52 | ``` |
29 | | - |
30 | | - ## USAGE |
31 | | - Sample mobile application for verifying interactions is available [here](https://github.com/mostroverkhov/rsocket-backport-demo) |
32 | | - |
33 | | - ### Client |
34 | | - Client initiates new `Connections`. Because protocol is duplex, each side of connection has |
35 | | - `Requester` RSocket for making requests to peer, and `Responder` RSocket to handle |
36 | | - requests from peer. `Responder` RSocket is optional for Client. |
37 | | - |
38 | | - ``` |
39 | | - val rSocket: Single<RSocket> = RSocketFactory // Requester RSocket |
| 53 | +#### Interactions |
| 54 | + Fire and Forget |
| 55 | + `RSocket.fireAndForget(payload: Payload): Completable` |
| 56 | + Request-Response |
| 57 | + `RSocket.requestResponse(payload: Payload): Single<Payload>` |
| 58 | + Request-Stream |
| 59 | + `RSocket.requestStream(payload: Payload): Flowable<Payload>` |
| 60 | + Request-Channel |
| 61 | + `RSocket.requestChannel(payload: Publisher<Payload>): Flowable<Payload>` |
| 62 | + Metadata-Push |
| 63 | + `fun metadataPush(payload: Payload): Completable` |
| 64 | + |
| 65 | +#### Client |
| 66 | + Client is initiator of `Connections` |
| 67 | + ```kotlin |
| 68 | + val rSocket: Single<RSocket> = RSocketFactory // Requester RSocket |
40 | 69 | .connect() |
41 | | - .acceptor { -> handler() } // Optional responder RSocket |
| 70 | + .acceptor { { requesterRSocket -> handler(requesterRSocket) } } // Optional handler RSocket |
42 | 71 | .transport(OkhttpWebsocketClientTransport // WebSockets transport |
43 | | - .create(protocol, host, port)) |
| 72 | + .create(protocol, host, port)) |
44 | 73 | .start() |
45 | | - |
46 | | - private fun handler(): RSocket { |
| 74 | + |
| 75 | + private fun handler(requester:RSocket): RSocket { |
47 | 76 | return object : AbstractRSocket() { |
48 | | - override fun fireAndForget(payload: Payload): Completable { |
49 | | - return Completable.fromAction {Log.d("tag", "fire-n-forget from server")} |
| 77 | + override fun requestStream(payload: Payload): Flowable<Payload> { |
| 78 | + return Flowable.just(PayloadImpl.textPayload("client handler response")) |
50 | 79 | } |
51 | 80 | } |
52 | 81 | } |
53 | | - ``` |
54 | | - Messages for all 4 interactions are represented as `Payload` of binary (nio `ByteBuffer`) data |
55 | | - and metadata (to/from UTF8-string utility methods are available) |
56 | | - |
57 | | - Request-stream |
58 | | - ``` |
59 | | - rSocket.flatMapPublisher { |
60 | | - it.requestStream(PayloadImpl("req-stream ping")) |
61 | | - }.subscribe { responsePayload -> Log.d("request-stream", responsePayload.getDataUtf8)} |
62 | | - ``` |
63 | | - |
64 | | - Request-channel |
65 | | - ``` |
66 | | - rSocket.flatMapPublisher { |
67 | | - it.requestChannel(Flowable.just( |
68 | | - PayloadImpl("req-channel1"), |
69 | | - PayloadImpl("req-channel2"))) |
70 | | - }.subscribe { responsePayload -> Log.d("request-stream", responsePayload.getDataUtf8)} |
71 | | - ``` |
72 | | - ### Server |
73 | | - Server accepts new `Connections` from peers. Same as `Client` it has `Requester` and `Responder` RSockets. |
74 | | - As this project does not provide server implementation, use [RSocket-java](https://github.com/rsocket/rsocket-java) with `Netty` based `WebSockets` |
75 | | - transport. Check its [examples](https://github.com/rsocket/rsocket-java/tree/1.0.x/rsocket-examples) folder or sample [app](https://github.com/mostroverkhov/rsocket-backport-demo/tree/master/rsocket-server-netty) minimalistic server |
76 | | - |
| 82 | +``` |
| 83 | +#### Server |
| 84 | +Accepts `Connections` from `Clients` |
| 85 | +```kotlin |
| 86 | +val closeable: Single<Closeable> = RSocketFactory |
| 87 | + .receive() |
| 88 | + .acceptor { { setup, rSocket -> handler(setup, rSocket) } } // server handler RSocket |
| 89 | + .transport(WebsocketServerTransport.create(port)) // Netty websocket transport |
| 90 | + .start() |
| 91 | + |
| 92 | + |
| 93 | +private fun handler(setup: Setup, rSocket: RSocket): Single<RSocket> { |
| 94 | + return Single.just(object : AbstractRSocket() { |
| 95 | + override fun requestStream(payload: Payload): Flowable<Payload> { |
| 96 | + return Flowable.just(PayloadImpl.textPayload("server handler response")) |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | +``` |
| 102 | + |
| 103 | +### LICENSE |
| 104 | + |
| 105 | +Copyright 2015-2018 Netflix, Inc. |
| 106 | +Maksym Ostroverkhov |
| 107 | + |
| 108 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 109 | +you may not use this file except in compliance with the License. |
| 110 | +You may obtain a copy of the License at |
| 111 | + |
| 112 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 113 | + |
| 114 | +Unless required by applicable law or agreed to in writing, software |
| 115 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 116 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 117 | +See the License for the specific language governing permissions and |
| 118 | +limitations under the License. |
0 commit comments