API reference

Tauri commands

Every Tauri command of the plugin with its arguments, generated from src/commands.rs.

The #[tauri::command] functions in src/commands.rs, which the JS package calls with invoke('plugin:centrifugo|<name>', args). You normally use the JavaScript API instead; this list is for calling commands directly. From Rust, see Rust backend. Arguments are sent in camelCase; the Window parameter is injected by Tauri and not listed.

CommandDescription
connectEstablish a connection to Centrifugo server
disconnectClose the connection to Centrifugo server
set_tokenSet or update the authentication token
provide_tokenAnswer a pending centrifugo:need_token request
publishPublish a message to a specific channel
rpcExecute an RPC method on the Centrifugo server
presenceGet presence information for a channel
presence_statsGet presence statistics for a channel
historyGet message history for a channel
sendSend a message directly to the server
refreshAnswer a pending need_token request
sub_refreshAnswer a pending need_sub_token request for a channel
pingDeprecated: check that the client is connected
is_connectedCheck if currently connected to Centrifugo server
get_subscriptionsGet current channel subscriptions
get_connection_stateGet detailed connection state
add_subscriptionAdd a new channel subscription
add_subscription_with_optionsAdd a channel subscription with options
remove_subscriptionRemove a channel subscription
remove_all_subscriptionsRemove every channel subscription

Commands

connect

Establish a connection to Centrifugo server

Closes a previous client, creates a new one, subscribes to config.channels and starts connecting. Returns at once; progress arrives as events, and the SDK reconnects with backoff by itself.

Events Emitted

  • centrifugo:connecting - Connection attempt started
  • centrifugo:connected - Connection established
  • centrifugo:subscribed - Channel subscriptions successful
  • centrifugo:error - Connection or subscription errors
invoke('plugin:centrifugo|connect', { config })
Argument (JS)Rust parameterType
configconfigStartConfig

Returns (). Rust: async fn. Permission: centrifugo:allow-connect.

disconnect

Close the connection to Centrifugo server

Closes the connection and drops the client with all its subscriptions.

Events Emitted

  • centrifugo:disconnected - Connection closed
invoke('plugin:centrifugo|disconnect')

No arguments.

Returns (). Rust: async fn. Permission: centrifugo:allow-disconnect.

set_token

Set or update the authentication token

Replaces the connection token; the SDK uses it from the next connect attempt. Fails with NotConnected when there is no client.

invoke('plugin:centrifugo|set_token', { token })
Argument (JS)Rust parameterType
tokentokenString

Returns (). Rust: async fn. Permission: centrifugo:allow-set-token.

provide_token

Answer a pending centrifugo:need_token request

An empty token means "no token": anonymous on connect, unauthorized on refresh. Fails when no request is pending.

invoke('plugin:centrifugo|provide_token', { token })
Argument (JS)Rust parameterType
tokentokenString

Returns (). Rust: async fn. Permission: centrifugo:allow-provide-token.

publish

Publish a message to a specific channel

data is base64 unless dataEncoding is rawJson. Map publish (type/key/removed) is rejected with Not supported.

Requirements

  • Must be connected to Centrifugo server
  • The server must allow client publishing in the channel
invoke('plugin:centrifugo|publish', { request })
Argument (JS)Rust parameterType
requestrequestPublishRequest

Returns (). Rust: async fn. Permission: centrifugo:allow-publish.

rpc

Execute an RPC method on the Centrifugo server

This command sends an RPC request and waits for the response. The request data should be base64-encoded.

Requirements

  • Must be connected to Centrifugo server
invoke('plugin:centrifugo|rpc', { request })
Argument (JS)Rust parameterType
requestrequestRpcRequest

Returns String. Rust: async fn. Permission: centrifugo:allow-rpc.

presence

Get presence information for a channel

Returns the client IDs currently present in the channel.

invoke('plugin:centrifugo|presence', { request })
Argument (JS)Rust parameterType
requestrequestPresenceRequest

Returns Vec<String>. Rust: async fn. Permission: centrifugo:allow-presence.

presence_stats

Get presence statistics for a channel

Returns aggregated statistics about the number of clients and users in a channel.

invoke('plugin:centrifugo|presence_stats', { request })
Argument (JS)Rust parameterType
requestrequestPresenceStatsRequest

Returns (u32, u32). Rust: async fn. Permission: centrifugo:allow-presence-stats.

history

Get message history for a channel

Retrieves publications from the channel history (limit, reverse; since is ignored). The channel must have history enabled.

invoke('plugin:centrifugo|history', { request })
Argument (JS)Rust parameterType
requestrequestHistoryRequest

Returns Vec<PublicationData>. Rust: async fn. Permission: centrifugo:allow-history.

send

Send a message directly to the server

Sends an async message to the server (not to a channel, no reply).

invoke('plugin:centrifugo|send', { request })
Argument (JS)Rust parameterType
requestrequestSendRequest

Returns (). Rust: async fn. Permission: centrifugo:allow-send.

refresh

Answer a pending need_token request

Answers a pending centrifugo:need_token request with the token; when none is pending, sets the token for the next connect. The SDK refreshes tokens itself before they expire.

invoke('plugin:centrifugo|refresh', { request })
Argument (JS)Rust parameterType
requestrequestRefreshRequest

Returns (). Rust: async fn. Permission: centrifugo:allow-refresh.

sub_refresh

Answer a pending need_sub_token request for a channel

Answers a pending centrifugo:need_sub_token request for request.channel. Fails when none is pending. Track/untrack fields are rejected with Not supported; signature is ignored.

invoke('plugin:centrifugo|sub_refresh', { request })
Argument (JS)Rust parameterType
requestrequestSubRefreshRequest

Returns (). Rust: async fn. Permission: centrifugo:allow-sub-refresh.

ping

Deprecated: check that the client is connected

The SDK keeps the connection alive with server pings and has no client ping; this only fails with NotConnected when not connected.

invoke('plugin:centrifugo|ping')

No arguments.

Returns (). Rust: async fn. Permission: centrifugo:allow-ping.

is_connected

Check if currently connected to Centrifugo server

Returns the current connection status without establishing a connection.

invoke('plugin:centrifugo|is_connected')

No arguments.

Returns bool. Rust: async fn. Permission: centrifugo:allow-is-connected.

get_subscriptions

Get current channel subscriptions

Returns the channels added through the plugin; the value is true while subscribed.

invoke('plugin:centrifugo|get_subscriptions')

No arguments.

Returns HashMap<String, bool>. Rust: async fn. Permission: centrifugo:allow-get-subscriptions.

get_connection_state

Get detailed connection state

One of connecting, connected, closed, disconnected (also when there is no client).

invoke('plugin:centrifugo|get_connection_state')

No arguments.

Returns String. Rust: async fn. Permission: centrifugo:allow-get-connection-state.

add_subscription

Add a new channel subscription

Subscribes to a channel with default options. Fails with NotConnected when there is no client.

Events Emitted

  • centrifugo:subscribing - Subscription attempt started
  • centrifugo:subscribed - Subscription successful
  • centrifugo:error - Subscription errors
invoke('plugin:centrifugo|add_subscription', { channel })
Argument (JS)Rust parameterType
channelchannelString

Returns (). Rust: async fn. Permission: centrifugo:allow-add-subscription.

add_subscription_with_options

Add a channel subscription with options

Token (private channel, refreshed via centrifugo:need_sub_token), recovery, join/leave, positioning, fossil delta, resume position.

Events Emitted

  • centrifugo:subscribing, centrifugo:subscribed, centrifugo:error
invoke('plugin:centrifugo|add_subscription_with_options', { options })
Argument (JS)Rust parameterType
optionsoptionsSubscribeOptions

Returns (). Rust: async fn. Permission: centrifugo:allow-add-subscription-with-options.

remove_subscription

Remove a channel subscription

Unsubscribes from a channel and forgets it. Unknown channels are ignored.

Events Emitted

  • centrifugo:unsubscribed - Unsubscription successful
invoke('plugin:centrifugo|remove_subscription', { channel })
Argument (JS)Rust parameterType
channelchannelString

Returns (). Rust: async fn. Permission: centrifugo:allow-remove-subscription.

remove_all_subscriptions

Remove every channel subscription

invoke('plugin:centrifugo|remove_all_subscriptions')

No arguments.

Returns (). Rust: async fn. Permission: centrifugo:allow-remove-all-subscriptions.