Guides

Events

Every centrifugo:* event, its payload, and the listener helpers.

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

EventHelperPayload
connectingonConnectingts, code, reason. On the first connect and on every reconnect.
connectedonConnectedts, client (client ID).
disconnectedonDisconnectedts, code, reason. No more reconnects until connect().
erroronErrorerror (text); channel too when a subscription failed.
need_tokenonNeedTokents. 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.

EventHelperPayload
subscribingonSubscribingchannel, code, reason. Also on resubscribe after a reconnect.
subscribedonSubscribedchannel, wasRecovering, recovered, streamPosition ({ offset, epoch } or null).
unsubscribedonUnsubscribedchannel, code, reason.
publicationonPublicationPublicationData
join / leave—channel, info (client, user, connInfo, chanInfo; the last two base64). Needs joinLeave: true.
need_sub_tokenonNeedSubTokenchannel, ts. Answer with subRefresh.

Server-side subscriptions

Channels the server subscribes the connection to (from the connection JWT channels claim or the server config).

EventHelperPayload
server_subscribing—channel
server_subscribed—channel
server_unsubscribed—channel
server_publicationonServerPublicationPublicationData
server_join / server_leave—channel, client, user

Publication payload

FieldMeaning
channelChannel of the publication.
dataThe data, always base64 (dataEncoding: 'base64').
rawJsonThe data parsed as JSON, when it is valid JSON.
offsetOffset in the channel stream.
infoPublisher (client, user, base64 connInfo/chanInfo), when the server sends it.
tagsPublication tags.
timestampPublication 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.

HelperCallback 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.

In 0.2.0 the 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.