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.
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.
Library available in Maven: https://search.maven.org/artifact/io.github.centrifugal/centrifuge-java
http://www.javadoc.io/doc/io.github.centrifugal/centrifuge-java
Connect to server based on Centrifuge library:
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.
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:
...
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:
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 or in demo Android app
To use with Android don't forget to set INTERNET permission to AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET" />
- connect to server using JSON protocol format
- connect to server using Protobuf protocol format
- connect with JWT
- connect with custom header
- automatic reconnect in case of errors, network problems etc
- exponential backoff for reconnect
- connect and disconnect events
- handle disconnect reason
- subscribe on channel and handle asynchronous Publications
- handle Join and Leave messages
- handle Unsubscribe notifications
- reconnect on subscribe timeout
- publish method of Subscription
- unsubscribe method of Subscription
- presence method of Subscription
- presence stats method of Subscription
- history method of Subscription
- send asynchronous messages to server
- handle asynchronous messages from server
- send RPC commands
- publish to channel without being subscribed
- subscribe to private channels with JWT
- connection JWT refresh
- private channel subscription JWT refresh
- handle connection expired error
- handle subscription expired error
- ping/pong to find broken connection
- server-side subscriptions
- message recovery mechanism
protoc --java_out=./ client.proto
mv io/github/centrifugal/centrifuge/internal/protocol centrifuge/src/main/java/io/github/centrifugal/centrifuge/internal/protocol
rm -r io/
Library is available under the MIT license. See LICENSE for details.
- Thanks to Maks Atygaev for initial library boilerplate
Bump version in centrifuge/build/gradle
. Create new library tag. Then run:
./gradlew uploadArchives
Then follow instructions:
https://central.sonatype.org/pages/releasing-the-deployment.html
I.e.
- Login here: https://oss.sonatype.org/
- Go to
Staging repositories
- Find release, push
Close
button, wait - Push
Release
button