# Struct Client

> A Zaun client. Clones share one SimpleX client and one driver; methods may be called from any thread, and block until the host answers or the timeout passes.

`zaun_client::Client` · Struct

```rust
pub struct Client { … }
```

A Zaun client. Clones share one SimpleX client and one driver; methods may be called from
any thread, and block until the host answers or the timeout passes.

## Methods

### `set_presence`

```rust
pub fn set_presence(&self, status: PresenceStatus, activity: Option<Activity>) -> Result<()>
```

Sets this member's status and activity in every community: `Online`, `Idle`, `Dnd` or
`Invisible` (everyone else sees `Offline`). The host shows at most one change per 3 s;
the latest call wins and is sent again after each reconnection.

# Errors
`Error::Closed` once the client stopped.

### `builder`

```rust
pub fn builder<impl AsRef<Path>>(dir: impl AsRef<Path>) -> ClientBuilder
```

A client whose SimpleX store and community state live in `dir`.

### `events`

```rust
pub fn events(&self) -> impl Iterator<Item = Event> + Send + 'static
```

The events, blocking for the next one; ends when the client stops.

### `next_event`

```rust
pub fn next_event(&self, timeout: Duration) -> Option<Event>
```

The next event, or `None` when none arrives within `timeout` or the client stopped.

### `simplex`

```rust
pub fn simplex(&self) -> &Client
```

The SimpleX client underneath, for what Zaun does not wrap.

### `now_ms`

```rust
pub fn now_ms(&self) -> u64
```

Milliseconds since the epoch, from the same clock the sessions use.

### `communities`

```rust
pub fn communities(&self) -> Vec<Community>
```

The communities this client belongs to.

### `community`

```rust
pub fn community(&self, id: CommunityId) -> Community
```

The community `id`, as an object that acts for itself.

### `join`

```rust
pub fn join(&self, link: &str, display_name: &str) -> Result<Community>
```

Joins a community with an invitation link (`zaun://join/…`) under `display_name`, and
returns it once the host admitted this member and its cache loaded (`Event::Ready`
still comes). A member pending approval is admitted later, by a moderator.

# Errors
`Error::BadLink`, a refusal (expired, used up, banned), or a timeout.

### `create_community`

```rust
pub fn create_community(&self, host_address: &str, host_key: PublicKey, name: &str, display_name: &str) -> Result<Community>
```

Creates a community named `name` on the host at `host_address` whose public key is
`host_key` (what `zaun-host id` prints), as its owner `display_name`. The community's
root key is made here and kept in `<dir>/zaun/<community>/root.key` (mode 0600): it
is the community's identity, so back it up. The host must allow the creation.

# Errors
The host refused (creation closed) or did not answer.

### `create_community_with_root`

```rust
pub fn create_community_with_root(&self, host_address: &str, host_key: PublicKey, name: &str, display_name: &str, root: SecretKey) -> Result<Community>
```

`create_community` with a root key made beforehand, so its
community id (`protocol::crypto::community_id`) can be allow-listed on the host first.

# Errors
As `create_community`.

### `command`

```rust
pub fn command<B>(&self, community: CommunityId, channel: Option<ChannelId>, body: B) -> Result<<B as >::Response>
```

Sends any command and decodes its answer: the escape hatch for every command without a
typed method. `channel` is the envelope's channel, for channel commands.

# Errors
`Error::Refused` with the host's error, `Error::UnknownCommunity`, or a timeout.

### `command_named`

```rust
pub fn command_named(&self, community: CommunityId, channel: Option<ChannelId>, name: &str, body: Value) -> Result<Value>
```

Sends the command called `name` (`"channel.update"`, …) with its body as JSON (the
specification's field names) and answers the response body as JSON: `command`
for a program that picks commands at run time. The body is checked as that command's
type before it is sent. `session.hello` and signals are not sent this way.

[`command`]: Self::command

# Errors
`Error::Protocol` for an unknown name or a body that does not fit it; then as
`command`.

### `close`

```rust
pub fn close(&self)
```

Stops the driver and the SimpleX client after saving state. Other clones stop too.

### `moderate_voice`

```rust
pub fn moderate_voice(&self, community: CommunityId, action: Moderate) -> Result<()>
```

Server-mutes, server-deafens, moves or disconnects a member in voice
(`voice.moderate`; `MUTE_MEMBERS`, `DEAFEN_MEMBERS`, `MOVE_MEMBERS`).

# Errors
The host refused (missing permission, rank) or did not answer.

## Trait implementations

`Clone`

[Full rustdoc page](/docs/api/rust/zaun-client/zaun_client/client/struct.Client.html)
