Guides

Publish, RPC, history, presence

Calling the server - publish, RPC, async send, history, presence and presence stats.

All of these need a connected client: they reject with NotConnected without a client and wait (up to readTimeout) while it is connecting. What a client may do is decided by the server config (permissions per namespace) or by your proxy; a refusal comes back as CentrifugoError.

Data goes as base64 unless you set dataEncoding: 'rawJson'; the helpers do the encoding for you. See Payloads.

Publish

import { publish, helpers, utils } from "tauri-plugin-centrifugo-api";

await helpers.publishJson("chat", { text: "hi" });        // JSON, sent as base64
await helpers.publishRawJson("chat", { text: "hi" });     // JSON, sent as is
await publish({ channel: "chat", data: utils.encode("plain text") });

The client does not need to be subscribed to the channel; the server must allow client publishing there (allow_publish_for_client, allow_publish_for_subscriber, or a publish proxy). Map publish fields (type, key, removed) are rejected, see Deprecations.

RPC

import { rpc, helpers, utils } from "tauri-plugin-centrifugo-api";

const profile = await helpers.rpcJson<{ id: number }, { name: string }>("getProfile", { id: 1 });

const b64 = await rpc({ method: "getProfile", data: utils.encodeJson({ id: 1 }) });
const same = utils.decodeJson(b64);

RPC needs an RPC proxy on the server. The result is always base64 (rpc, helpers.rpcRawJson); helpers.rpcJson decodes it as JSON.

Send

import { send, utils } from "tauri-plugin-centrifugo-api";

await send({ data: utils.encodeJson({ type: "typing" }) });

An async message to the server, no reply. It reaches your backend only through a send proxy. Messages the server sends to the client are not forwarded as events.

History

import { history, utils } from "tauri-plugin-centrifugo-api";

const pubs = await history({ channel: "chat", limit: 20, reverse: true }); // newest first
for (const p of pubs) console.log(p.offset, p.rawJson ?? utils.decode(p.data));

Returns PublicationData items like the publication event. The channel needs history on the server (history_size, history_ttl) and the client needs permission to read it. since is ignored; to continue from a position, subscribe with since.

Presence

import { presence, presenceStats } from "tauri-plugin-centrifugo-api";

const clientIds = await presence({ channel: "chat" });              // string[]
const [numClients, numUsers] = await presenceStats({ channel: "chat" });

The channel needs presence enabled on the server.