Skip to content

Connecting and joining

  1. Start a client. A client keeps its keys and state in a store sealed with a passphrase.

    import fs from "node:fs";
    import { Client } from "zaun-client";
    const file = "bot.snapshot";
    const snapshot = fs.existsSync(file) ? fs.readFileSync(file) : undefined;
    const client = await Client.start({ name: "Ping bot", key: process.env.ZAUN_KEY, snapshot });
    client.on("change", () => fs.writeFileSync(file, client.save())); // keep the store

    Without relays named, the official SimpleX servers are used. Name your own with .relay("smp://…") (Rust) or relays: ["smp://…"] (JavaScript).

  2. Join a community with an invite link, only the first time. Later runs reconnect from the saved state.

    if (client.saved.size === 0) {
    await client.join(process.env.ZAUN_INVITE, "Ping bot"); // the link is a secret: never log it
    }

    Joining opens a post-quantum SimpleX connection to the host, checks the host’s signed proof that it serves the community, makes your member key and registers it, then loads the community into the local cache.

  3. Handle events.

    client.on("ready", (community) => console.log(`ready in ${community.name}`));
    client.on("messageCreate", async (message) => {
    if (message.mine) return;
    if (message.content === "!ping") await message.reply("pong");
    });

See the complete ping bot example, in Rust and JavaScript.

An owner creates a community on a host that allows it (see First run): client.create_community(address, host_key, name, display_name) in Rust, client.createCommunity({ host, hostKey, name, displayName }) in JavaScript. The community’s root key is saved in the client’s store: back it up.