Skip to content
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

Support client protocol v2 #32

Merged
merged 20 commits into from
Jul 20, 2022
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
138 changes: 6 additions & 132 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# centrifuge-java

This is a Websocket client for Centrifugo and Centrifuge library. Client uses Protobuf protocol for client-server communication. `centrifuge-java` runs all operations in its own threads and provides necessary callbacks so you don't need to worry about managing concurrency yourself.
Websocket client for [Centrifugo](https://github.com/centrifugal/centrifugo) server and [Centrifuge](https://github.com/centrifugal/centrifuge) library.

## Status of library
There is no v1 release of this library yet – API still evolves. At the moment patch version updates only contain backwards compatible changes, minor version updates can have backwards incompatible API changes.

This library is feature rich and supports almost all available Centrifuge/Centrifugo features (see matrix below). But it's very young and not tested in production application yet. Any help and feedback is very appreciated to make it production ready and update library status. Any report will give us an understanding that the library works, is useful and we should continue developing it. Please share your stories.
Check out [client SDK API specification](https://centrifugal.dev/docs/transports/client_api) to learn how this SDK behaves. It's recommended to read that before starting to work with this SDK as the spec covers common SDK behavior - describes client and subscription state transitions, main options and methods. Also check out examples folder.

The features implemented by this SDK can be found in [SDK feature matrix](https://centrifugal.dev/docs/transports/client_sdk#sdk-feature-matrix).

## Installation

Expand All @@ -28,97 +30,7 @@ More information [about Android shrinking](https://developer.android.com/studio/

## Basic usage

Connect to server based on Centrifuge library:

```java
EventListener listener = new EventListener() {
@Override
public void onConnect(Client client, ConnectEvent event) {
System.out.println("connected");
}

@Override
public void onDisconnect(Client client, DisconnectEvent event) {
System.out.printf("disconnected %s, reconnect %s%n", event.getReason(), event.getReconnect());
}
};

Client client = new Client(
"ws://localhost:8000/connection/websocket?format=protobuf",
new Options(),
listener
);
client.connect();
```

Note that **you must use** `?format=protobuf` in connection URL as this client communicates with Centrifugo/Centrifuge over Protobuf protocol. While this client uses binary Protobuf protocol nothing stops you from sending JSON-encoded data over it.

Also in case of running in Android emulator don't forget to use proper connection address to Centrifuge/Centrifugo (as `localhost` is pointing to emulator vm and obviously your server instance is not available there).

To connect to Centrifugo you need to additionally set connection JWT:

```java
...
Client client = new Client(
"ws://localhost:8000/connection/websocket?format=protobuf",
new Options(),
listener
);
client.setToken("YOUR CONNECTION JWT")
client.connect()
```

Now let's look at how to subscribe to channel and listen to messages published into it:

```java
EventListener listener = new EventListener() {
@Override
public void onConnect(Client client, ConnectEvent event) {
System.out.println("connected");
}

@Override
public void onDisconnect(Client client, DisconnectEvent event) {
System.out.printf("disconnected %s, reconnect %s%n", event.getReason(), event.getReconnect());
}
};

SubscriptionEventListener subListener = new SubscriptionEventListener() {
@Override
public void onSubscribeSuccess(Subscription sub, SubscribeSuccessEvent event) {
System.out.println("subscribed to " + sub.getChannel());
}
@Override
public void onSubscribeError(Subscription sub, SubscribeErrorEvent event) {
System.out.println("subscribe error " + sub.getChannel() + " " + event.getMessage());
}
@Override
public void onPublish(Subscription sub, PublishEvent event) {
String data = new String(event.getData(), UTF_8);
System.out.println("message from " + sub.getChannel() + " " + data);
}
}

Client client = new Client(
"ws://localhost:8000/connection/websocket?format=protobuf",
new Options(),
listener
);
// If using Centrifugo.
client.setToken("YOUR CONNECTION JWT")
client.connect()

Subscription sub;
try {
sub = client.newSubscription("chat:index", subListener);
} catch (DuplicateSubscriptionException e) {
e.printStackTrace();
return;
}
sub.subscribe();
```

See more example code in [console Java example](https://github.com/centrifugal/centrifuge-java/blob/master/example/src/main/java/io/github/centrifugal/centrifuge/example/Main.java) or in [demo Android app](https://github.com/centrifugal/centrifuge-java/blob/master/demo/src/main/java/io/github/centrifugal/centrifuge/demo/MainActivity.java)
See example code in [console Java example](https://github.com/centrifugal/centrifuge-java/blob/master/example/src/main/java/io/github/centrifugal/centrifuge/example/Main.java) or in [demo Android app](https://github.com/centrifugal/centrifuge-java/blob/master/demo/src/main/java/io/github/centrifugal/centrifuge/demo/MainActivity.java)

To use with Android don't forget to set INTERNET permission to `AndroidManifest.xml`:

Expand All @@ -134,44 +46,6 @@ When a mobile application goes to the background there are OS-specific limitatio

[![Build Status](https://travis-ci.org/centrifugal/centrifuge-java.svg)](https://travis-ci.org/centrifugal/centrifuge-java)

## Feature matrix

- [ ] connect to server using JSON protocol format
- [x] connect to server using Protobuf protocol format
- [x] connect with JWT
- [x] connect with custom header
- [x] automatic reconnect in case of errors, network problems etc
- [x] exponential backoff for reconnect
- [x] connect and disconnect events
- [x] handle disconnect reason
- [x] subscribe on channel and handle asynchronous Publications
- [x] handle Join and Leave messages
- [x] handle Unsubscribe notifications
- [x] reconnect on subscribe timeout
- [x] publish method of Subscription
- [x] unsubscribe method of Subscription
- [x] presence method of Subscription
- [x] presence stats method of Subscription
- [x] history method of Subscription
- [x] top-level publish method
- [x] top-level presence method
- [x] top-level presence stats method
- [x] top-level history method
- [ ] top-level unsubscribe method
- [x] send asynchronous messages to server
- [x] handle asynchronous messages from server
- [x] send RPC commands
- [x] subscribe to private channels with token (JWT)
- [x] connection JWT refresh
- [ ] private channel subscription token (JWT) refresh
- [ ] handle connection expired error
- [ ] handle subscription expired error
- [x] ping/pong to find broken connection
- [x] server-side subscriptions
- [x] message recovery mechanism for client-side subscriptions (works with Centrifugo >= 2.5.0 with `v3_use_offset` option set to `true`)
- [x] message recovery mechanism for server-side subscriptions
- [x] history stream pagination

## License

Library is available under the MIT license. See LICENSE for details.
Expand Down
9 changes: 3 additions & 6 deletions centrifuge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ apply plugin: 'signing'
apply plugin: 'java-library'

task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}

task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}

Expand All @@ -18,13 +16,12 @@ artifacts {

group = "io.github.centrifugal"
archivesBaseName = "centrifuge-java"
version = "0.1.0"
version = "0.2.0"

dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.14.9'
implementation 'com.google.protobuf:protobuf-javalite:3.11.4'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.google.protobuf:protobuf-javalite:3.19.4'
implementation 'net.sourceforge.streamsupport:streamsupport-cfuture:1.7.0'
implementation 'com.google.code.gson:gson:2.8.6'

testImplementation 'junit:junit:4.12'
}
Expand Down
Loading