Quick start
The client lives in Rust; the frontend starts it with connect() and gets everything back as
events. Set up the listeners first, then connect.
import {
connect, disconnect, addSubscription,
onConnecting, onConnected, onDisconnected, onError,
onSubscribed, onPublication, helpers, utils,
} from "tauri-plugin-centrifugo-api";
const unlisten = [
await onConnecting(({ payload }) => console.log("connecting", payload.code, payload.reason)),
await onConnected(({ payload }) => console.log("connected, client", payload.client)),
await onDisconnected(({ payload }) => console.log("disconnected", payload.code, payload.reason)),
await onError(({ payload }) => console.error(payload.channel ?? "client", payload.error)),
await onSubscribed(({ payload }) => console.log("subscribed to", payload.channel)),
await onPublication(({ payload }) => {
// `rawJson` is set when the data is valid JSON; `data` is always base64.
console.log(payload.channel, payload.rawJson ?? utils.decode(payload.data));
}),
];
await connect({
url: "ws://localhost:8000/connection/websocket",
token: "<connection JWT>", // omit for anonymous connections
channels: ["news"], // subscribed with default options
useProtobuf: false,
});
// connect() returns at once; wait for the `connected` state before calling the server.
if (await helpers.waitForConnection(5000)) {
await addSubscription("chat");
await helpers.publishJson("news", { text: "hello" });
}
// Later:
await disconnect();
unlisten.forEach((fn) => fn());
What next:
- Connection & config: every
connect()option, transports, state. - Tokens: JWTs that expire, tokens fetched by the frontend.
- Subscriptions: recovery,
since, delta, join/leave.
Example app
The repository has a Vue example app and a Centrifugo config for it:
centrifugo --config examples/server/centrifugo.json # listens on :8001
cd examples/tauri-app
pnpm install
pnpm tauri dev
Connect it to ws://localhost:8001/connection/websocket. The config allows anonymous clients to
subscribe, publish and read history, and enables presence, join/leave and HTTP streaming — for
local testing only.
Development
cargo build && cargo test # scenario tests, offline and against a real Centrifugo
pnpm build && pnpm test # guest-js: build + dist checks, unit tests
cargo mutants # mutation testing (cargo install cargo-mutants)
The tests in tests/ drive the plugin over IPC, as the JS API does. They test
scenarios, not single functions: proptest generates
random command sequences (connect, subscribe, publish, history, presence, tokens, ...) for one
app without a server and for two apps against a real Centrifugo, and after every step compares
replies, events, history and presence with a small model; a failing sequence is shrunk to the
shortest one. Stories cover what random steps can't reach: token refresh, server-side
subscriptions, transport failures and the HTTP-streaming fallback; each story is a short test
with a Given/When/Then comment:
| File | Scenarios |
|---|---|
offline_model.rs | random command sequences, no server |
server_model.rs | random command sequences of two apps against Centrifugo |
tokens.rs | token from the frontend, refresh before expiry |
server_side.rs | join/leave, personal server-side channel, server disconnect and unsubscribe |
transport.rs | dead server, refused connection, HTTP-streaming fallback, send with no handler |
cargo test starts the centrifugo binary from PATH (or $CENTRIFUGO_BIN) and skips the
server tests when it is missing. CI runs everything on Linux, macOS and Windows against
Centrifugo v5.4.9 and builds for Android.
cargo-mutants checks that the tests catch changes to the code: it
mutates src/ and expects cargo test to fail for each mutant. Config and the few excluded
mutants (with the reason) are in .cargo/mutants.toml; it needs centrifugo as well, or the
server tests skip and most mutants survive. It runs locally only, not in CI.