7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | A high-level, batteries-included [Matrix](https://matrix.org/) client library |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | written in Rust. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | This crate seeks to be a general-purpose library for writing software using the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | Matrix [Client-Server API](https://spec.matrix.org/latest/client-server-api/) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | to communicate with a Matrix homeserver. If you're writing a typical Matrix |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | client or bot, this is likely the crate you need. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | However, the crate is designed in a modular way and depends on several other |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | lower-level crates. If you're attempting something more custom, you might be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | interested in these: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | - [`matrix_sdk_base`]: A no-network-IO client state machine which can be used |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | to embed a Matrix client into an existing network stack or to build a new |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | Matrix client library on top. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | - [`matrix_sdk_crypto`]: A no-network-IO encryption state machine which can be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | used to add Matrix E2EE support into an existing client or library. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | # Getting started |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | The central component you'll be interacting with is the [`Client`]. A basic use |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | case will include instantiating the client, logging in as a user, registering |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | some event handlers and then syncing. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | This is demonstrated in the example below. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | ```rust,no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | use matrix_sdk::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | Client, config::SyncSettings, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | ruma::{user_id, events::room::message::SyncRoomMessageEvent}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | #[tokio::main] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | async fn main() -> anyhow::Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | let alice = user_id!("@alice:example.org"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | let client = Client::builder().server_name(alice.server_name()).build().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | // First we need to log in. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | client.matrix_auth().login_username(alice, "password").send().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | client.add_event_handler(|ev: SyncRoomMessageEvent| async move { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | println!("Received a message {:?}", ev); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | // Syncing is important to synchronize the client state with the server. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | // This method will never return unless there is an error. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | client.sync(SyncSettings::default()).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | More examples can be found in the [examples] directory. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | ## Crate feature flags |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | The following crate feature flags are available: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | | Feature | Default | Description | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | | ---------------- | :-----: | ----------------------------------------------------------------------------------------------------------------------------------------------- | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | | `anyhow` | No | Better logging for event handlers that return `anyhow::Result` | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | | `e2e-encryption` | Yes | End-to-end encryption (E2EE) support | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | | `eyre` | No | Better logging for event handlers that return `eyre::Result` | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | | `js` | No | Enables JavaScript API usage on WASM (does nothing on other targets) | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | | `markdown` | No | Support for sending Markdown-formatted messages | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | | `qrcode` | Yes | QR code verification support | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | | `sqlite` | Yes | Persistent storage of state and E2EE data (optionally, if feature `e2e-encryption` is enabled), via SQLite available on system | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | | `bundled-sqlite` | No | Persistent storage of state and E2EE data (optionally, if feature `e2e-encryption` is enabled), via SQLite compiled and bundled with the binary | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | | `indexeddb` | No | Persistent storage of state and E2EE data (optionally, if feature `e2e-encryption` is enabled) for browsers, via IndexedDB | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | | `socks` | No | SOCKS support in the default HTTP client, [`reqwest`] | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | | `sso-login` | No | Support for SSO login with a local HTTP server | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | [`reqwest`]: https://docs.rs/reqwest/0.11.5/reqwest/index.html |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | ## Enabling logging |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | Users of the matrix-sdk crate can enable log output by depending on the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | `tracing-subscriber` crate and including the following line in their |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | application (e.g. at the start of `main`): |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | ```rust |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | tracing_subscriber::fmt::init(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | The log output is controlled via the `RUST_LOG` environment variable by |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | setting it to one of the `error`, `warn`, `info`, `debug` or `trace` levels. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | The output is printed to stdout. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | The `RUST_LOG` variable also supports a more advanced syntax for filtering |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | log output more precisely, for instance with crate-level granularity. For |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | more information on this, check out the [tracing_subscriber documentation]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | [examples]: https://github.com/matrix-org/matrix-rust-sdk/tree/main/examples/ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | [tracing_subscriber documentation]: https://tracing.rs/tracing_subscriber/filter/struct.envfilter |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | [`matrix_sdk_crypto`]: https://docs.rs/matrix-sdk-crypto/ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | [`matrix_sdk_base`]: https://docs.rs/matrix-sdk-base/ |