# Axum 與 Actix Web 的即時、等候室與聊天

> Hibira crate 可發送事件、簽署頻道訂閱與驗證 webhook、在 Axum 或 Actix Web 路由前加上等候室，並呼叫 Chat API 與 Approvals。

Source: https://hibira.io/zh-tw/guides/rust

Hibira crate 可發送事件、簽署頻道訂閱與驗證 webhook、在 Axum 或 Actix Web 路由前加上等候室，並呼叫 Chat API 與 Approvals。

Package: hibira

- 提供 Axum 與 Actix Web 的等候室 middleware；handler 會收到訪客的 pass。
- 私有與 presence 頻道授權、pusher-js 登入與 webhook 驗證；關閉 client feature 時不含任何網路程式碼。
- 同一個 crate 也提供 Chat API、Approvals 與 Live Chat 驗證訪客。

## 1. 安裝

```shell
cargo add hibira --features axum   # or --features actix
```

## 2. 設定

```shell
HIBIRA_APP_ID=YOUR_APP_ID
HIBIRA_KEY=YOUR_APP_KEY
HIBIRA_SECRET=YOUR_APP_SECRET
HIBIRA_HOST=ws.hibira.io
```

## 3. 使用

```rust
use std::sync::Arc;
use axum::{middleware, routing::get, Extension, Router};
use hibira::{waiting_room::axum_guard, Client, Config, Pass};

let config = Config::from_env()?;
let hibira = Client::new(config.clone());
hibira.trigger(&["orders"], "order.shipped", &serde_json::json!({ "id": 1042 })).await?;

let app = Router::new()
    .route("/drop", get(|Extension(pass): Extension<Pass>| async move { format!("You are number {}", pass.number) }))
    .layer(middleware::from_fn_with_state(Arc::new(config.waiting_room("presale")), axum_guard));
```

## 開始之前

- 應用程式的密鑰只會在建立應用程式或輪替密鑰時顯示一次。請只放在伺服器上，不要放進瀏覽器或遊戲的建置檔。
- 私人頻道與 presence 頻道需要後端提供授權端點，用應用程式密鑰簽署訂閱，做法和 pusher-js 相同。

- [crates.io](https://crates.io/crates/hibira)
- [GitHub](https://github.com/hibira-io/rust)
