Package io.github.centrifugal.centrifuge
Class Client
java.lang.Object
io.github.centrifugal.centrifuge.Client
-
Constructor Summary
ConstructorsConstructorDescriptionClient(String endpoint, Options opts, EventListener listener) Creates a new instance of Client. -
Method Summary
Modifier and TypeMethodDescriptionbooleanclose(long awaitMilliseconds) Close disconnects client from server and cleans up client resources including shutdown of internal executors.voidconnect()Start connecting to a server.voidDisconnect from server and do not reconnect.getState()getSubscription(String channel) Try to get Subscription from internal client registry.voidhistory(String channel, HistoryOptions opts, ResultCallback<HistoryResult> cb) History can get channel publication history (useful for server-side subscriptions).newSubscription(String channel, SubscriptionEventListener listener) Create new subscription to channel with SubscriptionEventListener, see also overloaded newSubscription which allows setting SubscriptionOptionsnewSubscription(String channel, SubscriptionOptions options, SubscriptionEventListener listener) Create new subscription to channel with SubscriptionOptions and SubscriptionEventListener.voidpresence(String channel, ResultCallback<PresenceResult> cb) voidpresenceStats(String channel, ResultCallback<PresenceStatsResult> cb) voidpublish(String channel, byte[] data, ResultCallback<PublishResult> cb) Publish data to channel without being subscribed to it.voidSay Client that Subscription should be removed from internal registry.voidrpc(String method, byte[] data, ResultCallback<RPCResult> cb) Send RPC with method to server, process result in callback.voidsend(byte[] data, CompletionCallback cb) Send asynchronous message with data to server.voidsetToken allows updating connection token.
-
Constructor Details
-
Client
Creates a new instance of Client. Client allows to allocate new Subscriptions to channels, automatically manages reconnects and re-subscriptions on temporary failures.- Parameters:
endpoint- : connection endpoint.opts- : client options.listener- : client event handler.
-
-
Method Details
-
getState
-
connect
public void connect()Start connecting to a server. -
disconnect
public void disconnect()Disconnect from server and do not reconnect. -
setToken
setToken allows updating connection token. -
close
Close disconnects client from server and cleans up client resources including shutdown of internal executors. Client is not usable after calling this method. If you only need to temporary disconnect from server use .disconnect() method.- Parameters:
awaitMilliseconds- is time in milliseconds to wait for executor termination (0 means no waiting).- Returns:
- boolean that indicates whether executor terminated in awaitMilliseconds. For zero awaitMilliseconds close always returns false.
- Throws:
InterruptedException
-
newSubscription
public Subscription newSubscription(String channel, SubscriptionOptions options, SubscriptionEventListener listener) throws DuplicateSubscriptionException Create new subscription to channel with SubscriptionOptions and SubscriptionEventListener.Offset persistence is enabled automatically. The SDK saves stream offsets to disk and recovers from them on restart, ensuring no messages are lost.
Default offset storage locations:
- macOS: ~/Library/Application Support/centrifugo/offsets/
- Linux: ~/.local/share/centrifugo/offsets/
- Windows: %APPDATA%/centrifugo/offsets/
With automatic fallback to temp directory or current directory if needed.
IMPORTANT: If you run multiple client processes on the same machine subscribing to the same channel, each process MUST use a different
offsetStorageDirviaSubscriptionOptions.setOffsetStorageDir(String)to avoid overwriting each other's offsets. Example:// Process 1 SubscriptionOptions opts = new SubscriptionOptions(); opts.setOffsetStorageDir("/data/offsets/worker-1"); Subscription sub = client.newSubscription("my-channel", opts, listener); // Process 2 SubscriptionOptions opts = new SubscriptionOptions(); opts.setOffsetStorageDir("/data/offsets/worker-2"); Subscription sub = client.newSubscription("my-channel", opts, listener);- Parameters:
channel- channel to create Subscription for.options- SubscriptionOptions, e.g. token, offsetStorageDir.listener- event handler for subscription events.- Returns:
- Subscription.
- Throws:
DuplicateSubscriptionException- if Subscription already exists in internal registry.
-
newSubscription
public Subscription newSubscription(String channel, SubscriptionEventListener listener) throws DuplicateSubscriptionException Create new subscription to channel with SubscriptionEventListener, see also overloaded newSubscription which allows setting SubscriptionOptions- Parameters:
channel- : to create Subscription for.listener- : to pass event handler.- Returns:
- Subscription.
- Throws:
DuplicateSubscriptionException- if Subscription already exists in internal registry.
-
getSubscription
Try to get Subscription from internal client registry. Can return null if Subscription does not exist yet.- Parameters:
channel- : channel to get subscription for.- Returns:
- Subscription
-
removeSubscription
Say Client that Subscription should be removed from internal registry. Subscription will be automatically unsubscribed before removing.- Parameters:
sub- : Subscription to remove from registry.
-
send
Send asynchronous message with data to server. Callback successfully completes if data written to connection. No reply from server expected in this case.- Parameters:
data- : custom data to publish.cb- : will be called as soon as data sent to the connection or error happened.
-
rpc
Send RPC with method to server, process result in callback.- Parameters:
method- : of RPC call.data- : a custom payload for RPC call.cb- : will be called as soon as rpc response received or error happened.
-
publish
Publish data to channel without being subscribed to it. Publish option should be enabled in Centrifuge/Centrifugo server configuration.- Parameters:
channel- : to publish into.data- : to publish.cb- : will be called as soon as publish response received or error happened.
-
history
History can get channel publication history (useful for server-side subscriptions).- Parameters:
channel- : to get history for.opts- : for history request.cb- : will be called as soon as response receive or error happened.
-
presence
-
presenceStats
-