Events
The backend emits Tauri events named centrifugo:<event> to all windows. The on* helpers wrap
listen(): the callback gets the whole Tauri event ({ event, payload, id }), the data is in
payload. Each helper resolves to an UnlistenFn.
import { onSubscribed, onCentrifugoEvent, type TauriEventWrapper } from "tauri-plugin-centrifugo-api";
const off = await onSubscribed(({ payload }) => {
console.log(payload.channel, payload.recovered, payload.streamPosition);
});
// Events without a helper:
type Join = { channel: string; info: { client: string; user: string; connInfo: string; chanInfo: string } };
const offJoin = await onCentrifugoEvent<TauriEventWrapper<Join>>("join", ({ payload }) => {
console.log(payload.info.user, "joined", payload.channel);
});
off();
offJoin();
Connection
| Event | Helper | Payload |
|---|---|---|
connecting | onConnecting | ts, code, reason. On the first connect and on every reconnect. |
connected | onConnected | ts, client (client ID). |
disconnected | onDisconnected | ts, code, reason. No more reconnects until connect(). |
error | onError | error (text); channel too when a subscription failed. |
need_token | onNeedToken | ts. Answer with provideToken, see Tokens. |
ts is the time of the event in ms since the epoch. code/reason come from the SDK or the
server: connecting with 0 (connect called), 1 (transport closed) or 2 (no ping);
disconnected with 0 (disconnect called), 1 (unauthorized) or a server disconnect code
(3500+ means "do not reconnect").
Client-side subscriptions
Subscriptions made with channels, addSubscription or addSubscriptionWithOptions.
| Event | Helper | Payload |
|---|---|---|
subscribing | onSubscribing | channel, code, reason. Also on resubscribe after a reconnect. |
subscribed | onSubscribed | channel, wasRecovering, recovered, streamPosition ({ offset, epoch } or null). |
unsubscribed | onUnsubscribed | channel, code, reason. |
publication | onPublication | PublicationData |
join / leave | — | channel, info (client, user, connInfo, chanInfo; the last two base64). Needs joinLeave: true. |
need_sub_token | onNeedSubToken | channel, ts. Answer with subRefresh. |
Server-side subscriptions
Channels the server subscribes the connection to (from the connection JWT channels claim or
the server config).
| Event | Helper | Payload |
|---|---|---|
server_subscribing | — | channel |
server_subscribed | — | channel |
server_unsubscribed | — | channel |
server_publication | onServerPublication | PublicationData |
server_join / server_leave | — | channel, client, user |
Publication payload
| Field | Meaning |
|---|---|
channel | Channel of the publication. |
data | The data, always base64 (dataEncoding: 'base64'). |
rawJson | The data parsed as JSON, when it is valid JSON. |
offset | Offset in the channel stream. |
info | Publisher (client, user, base64 connInfo/chanInfo), when the server sends it. |
tags | Publication tags. |
timestamp | Publication time from the server, else arrival time (ms). |
Decoded message helpers
These listen to publication and give the data already decoded: rawJson if present, else the
base64 decoded as JSON, else the base64 string as is. The callback gets the data, not the Tauri
event.
| Helper | Callback gets |
|---|---|
onMessage(cb) | { channel, data } |
onChannelMessage(channel, cb) | data of one channel |
onAnyMessage(cb) | { channel, data, timestamp } (Date.now() on arrival) |
onPublicationRawJson(cb) | { channel, data }, only for JSON data |
onServerPublicationRawJson(cb) | Same for server_publication |
helpers.subscribeToChannel(channel, cb) | addSubscription + onChannelMessage; resolves to the UnlistenFn |
helpers.subscribeToChannels(channels, cb) | Same for several channels; { channel, data }, an array of UnlistenFn |
Unlistening does not unsubscribe; call removeSubscription for that.
on* callbacks got only the payload, so these helpers threw on every publication.
This is fixed after 0.2.0; code written for 0.2.0 that reads e.channel should read e.payload.channel.