RxJavaWebsocketClient

RxJavaWebsocketClient is simple library for Websocket connection for java and Android. It is designed to be fast and fault tolerant.

License

License

Categories

Categories

RxJava Container Microservices Reactive libraries
GroupId

GroupId

com.appunite
ArtifactId

ArtifactId

websockets-rxjava
Last Version

Last Version

4.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

RxJavaWebsocketClient
RxJavaWebsocketClient is simple library for Websocket connection for java and Android. It is designed to be fast and fault tolerant.
Project URL

Project URL

https://github.com/jacek-marchwicki/JavaWebsocketClient
Source Code Management

Source Code Management

https://github.com/jacek-marchwicki/JavaWebsocketClient.git

Download websockets-rxjava

How to add to project

<!-- https://jarcasting.com/artifacts/com.appunite/websockets-rxjava/ -->
<dependency>
    <groupId>com.appunite</groupId>
    <artifactId>websockets-rxjava</artifactId>
    <version>4.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.appunite/websockets-rxjava/
implementation 'com.appunite:websockets-rxjava:4.0.1'
// https://jarcasting.com/artifacts/com.appunite/websockets-rxjava/
implementation ("com.appunite:websockets-rxjava:4.0.1")
'com.appunite:websockets-rxjava:jar:4.0.1'
<dependency org="com.appunite" name="websockets-rxjava" rev="4.0.1">
  <artifact name="websockets-rxjava" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.appunite', module='websockets-rxjava', version='4.0.1')
)
libraryDependencies += "com.appunite" % "websockets-rxjava" % "4.0.1"
[com.appunite/websockets-rxjava "4.0.1"]

Dependencies

compile (3)

Group / Artifact Type Version
com.squareup.okhttp3 : okhttp-ws jar 3.1.1
io.reactivex : rxjava jar 1.0.9
com.google.code.findbugs : jsr305 jar 2.0.1

test (4)

Group / Artifact Type Version
org.mockito : mockito-all jar 1.9.5
junit : junit jar 4.11
com.google.truth : truth jar 0.25
org.hamcrest : hamcrest-all jar 1.3

Project Modules

There are no modules declared in this project.

JavaWebsocketClient also for Android

JavaWebsocketClient is library is simple library for Websocket connection in rx for java and Android. It is designed to be fast and fault tolerant.

Currently we use okhttp3 for websocket connection because okhttp3 is simple and well tested solution.

Build Status

Release

Presentation of example

Content of the package

  • Example websockets server python twisted server
  • Rx-java websocket client library websockets-rxjava/
  • Rx-java websocket android example websockets-rxjava-example/

Reactive example

How to connect to server:

final Request request = new Request.Builder()
        .get()
        .url("ws://10.10.0.2:8080/ws")
        .build();
final Subscription subscribe = new RxWebSockets(new OkHttpClient(), request)
        .webSocketObservable()
        .subscribe(new Action1<RxEvent>() {
            @Override
            public void call(RxEvent rxEvent) {
                System.out.println("Event: " + rxEvent.toString());
            }
        });
Thread.sleep(10000);
subscribe.unsubscribe();

Send message on connected:

final Subscription subscription = new RxWebSockets(newWebSocket, request)
        .webSocketObservable()
        .subscribe(new Action1<RxEvent>() {
            @Override
            public void call(RxEvent rxEvent) {
                if (rxEvent instanceof RxEventConnected) {
                    Observable.just("response")
                            .compose(RxMoreObservables.sendMessage((RxEventConnected) rxEvent))
                            .subscribe();
                }
            }
        });
Thread.sleep(1000);
subscription.unsubscribe();

For examples look:

Rx-java with json parser

class YourMessage {
    public String response;
    public String error;
}

final Request request = new Request.Builder()
        .get()
        .url("ws://10.10.0.2:8080/ws")
        .build();
final RxWebSockets rxWebSockets = new RxWebSockets(new OkHttpClient(), request)
final ObjectSerializer serializer = new GsonObjectSerializer(new Gson(), Message.class)
final RxObjectWebSockets webSockets = new RxObjectWebSockets(rxWebSockets), serializer);
final Subscription subscription = webSockets.webSocketObservable()
        .compose(MoreObservables.filterAndMap(RxObjectEventMessage.class))
        .compose(RxObjectEventMessage.filterAndMap(YourMessage.class))
        .subscribe(new Action1<YourMessage>() {
            @Override
            public void call(YourMessage yourMessage) {
                System.out.println("your message: " + yourMessage.response);
            }
        });
Thread.sleep(1000);
subscription.unsubscribe();

Run example from gradle

To run example first run websocket server, than update url to your host in:

./gradlew :websockets-rxjava-example:installDebug

How to add to your project

to your gradle file:

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {

    // snapshot version
    compile 'com.github.jacek-marchwicki:JavaWebsocketClient:master-SNAPSHOT'

    // or use specific version
    compile 'com.github.jacek-marchwicki:JavaWebsocketClient:<look-on-release-tab>'
}

License

Copyright [2015] [Jacek Marchwicki <[email protected]>]

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.

Versions

Version
4.0.1
4.0.0
3.0.1
3.0.0
2.2.0
2.1.0
2.0.0