Getting started

Quick start

Connect, listen to events, subscribe, publish and clean up.

The client lives in Rust; the frontend starts it with connect() and gets everything back as events. Set up the listeners first, then connect.

main.ts
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:

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   # unit tests + end-to-end tests against a real Centrifugo
pnpm build && pnpm test     # guest-js: build + dist checks, unit tests

cargo test starts the centrifugo binary from PATH (or $CENTRIFUGO_BIN) and skips the end-to-end tests when it is missing. CI runs everything on Linux, macOS and Windows against Centrifugo v5.4.9 and builds for Android.