官方套件 · 自動化與基礎架構

Cloudflare Worker 範本,一鍵部署

現成的 Worker,跑在你自己的 Cloudflare 帳號、就在網站旁邊。按 Deploy to Cloudflare,填入 app 金鑰和密鑰,Worker 就會在邊緣幫你做一件事:替登入的使用者簽私人和 presence 頻道,或是讓訪客在等候室排隊。頻道、排隊和設定都留在你的 Hibira 帳號。

最快的方式:裝好 Hibira 外掛,貼上一段提示詞

在你的 AI 程式助手裝好外掛,再貼上提示詞。助手會幫你登入、需要時建立免費應用、寫好程式碼並送出測試事件。 GitHub ↗

  1. 安裝外掛

    claude plugin marketplace add hibira-io/agent-plugin
    claude plugin install hibira@hibira
  2. 貼上這段提示詞

    Add Hibira Cloudflare Workers (Cloudflare Worker templates) to this project.
    Follow https://hibira.io/guides/cloudflare, adapted to how this project is already set up.
    If I have no Hibira app yet, create a Free one that allows http://localhost so I can test right away.
    Keep the app key and secret in environment variables, add placeholders to .env.example, and never commit real values or send the secret to a browser.
    When you are done, send a test event, show me it arriving, and list the files you changed.

想自己動手? 下方的步驟如下:

  1. 安裝
  2. 使用
  3. 傳送事件

其他 AI 工具與外掛內容 →

  • 頻道驗證:讓沒有後端的網站和 App 也能用私人和 presence 頻道。它會檢查你現有登入服務發的 token(Clerk、Auth0、Supabase、Firebase、Cloudflare Access 或你自己的),只簽規則允許的頻道,例如每位使用者自己的 private-user 頻道。
  • 等候室:放在任何經過 Cloudflare 的頁面前面,不用改程式,任何 Cloudflare 方案都能用。
  • 沒有後端也能發送:設定 PUBLISH 後,登入的使用者可以透過頻道驗證 Worker 的 /publish 發送聊天訊息、投票和表情回應。發送者由 Worker 依登入 token 填入,沒辦法冒用別人的名義。
  • AI 串流:用 useChat() 做的 AI 聊天後端。Workers AI、Anthropic 或任何 OpenAI 相容模型的回答會透過頻道送到每個分頁,分頁關掉也會寫完,重新整理後能接著看。
  • 每個範本是同一個 repo 裡的一個資料夾,設定都有說明,也附上不用按鈕的部署方式。全部都在 Workers 免費方案的範圍內。

登入後,範例程式碼會自動帶入您的叢集主機。 登入

  1. 安裝

    https://deploy.workers.cloudflare.com/?url=https://github.com/hibira-io/cloudflare/tree/main/channel-auth
      → sign in to Cloudflare and GitHub
      → HIBIRA_KEY, HIBIRA_SECRET: from your app page
      → JWKS_URL: your login's public keys, for example
        https://YOUR-APP.clerk.accounts.dev/.well-known/jwks.json
      → CHANNELS: private-user-{id},presence-*
      → Deploy
  2. 使用

    import Pusher from 'pusher-js';
    
    const auth = 'https://hibira-channel-auth.YOUR-SUBDOMAIN.workers.dev';
    const headersProvider = () => ({ Authorization: `Bearer ${getToken()}` });
    
    const pusher = new Pusher('YOUR_APP_KEY', {
      cluster: 'hkg',
      wsHost: 'ws.hibira.io',
      enabledTransports: ['ws', 'wss'],
      channelAuthorization: { endpoint: `${auth}/pusher/auth`, headersProvider },
      userAuthentication: { endpoint: `${auth}/pusher/user-auth`, headersProvider },
    });
    
    pusher.subscribe(`private-user-${userId}`).bind('order.shipped', (order) => {
      console.log('Your order shipped', order);
    });
  3. 傳送事件

    // PUBLISH: presence-room-*   PUBLISH_EVENTS: message   HIBIRA_APP_ID: your app ID
    await fetch(`${auth}/publish`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', ...headersProvider() },
      body: JSON.stringify({ channel: 'presence-room-1', event: 'message', data: { text: 'Hello' } }),
    });
    
    // Everyone on the channel gets data.sender from the token: { id, name, picture }
    pusher.subscribe('presence-room-1').bind('message', ({ text, sender }) => show(sender.name, text));

開始之前

  • getToken() 回傳使用者目前的登入 token,例如 await clerk.session.getToken() 或 Supabase session 的 access_token。Worker 會先檢查簽章、到期時間、issuer 和 audience,才會簽名。
  • 在 CHANNELS 裡,{id} 是使用者 ID,{claim} 是 token 裡的任一個 claim,* 代表名稱的其餘部分。所以 private-org-{org_id}-* 可以讓同一個組織的成員共用組織的頻道。
  • app 密鑰存成 Worker secret,不會送到瀏覽器。Worker 不保存任何資料。
  • 「正在輸入」、游標這類漏掉也沒關係的東西,在 app 設定開啟用戶端事件後,可以直接從瀏覽器在私人或 presence 頻道上發送 client- 事件。
  • 因為資料變動才產生的訊息,最好從變動發生的地方發送:Supabase Edge Function、Firebase Cloud Function,或是 n8n、Make、Activepieces 與 Node-RED 的 Hibira 節點。
  • 用瀏覽器打開 Worker 自己的網址,就能看到設定是否完成、還缺哪些設定。頁面會用你的語言顯示,也不會顯示任何設定的值。

請使用儀表板中的應用程式 Key;Secret 只放在你的伺服器上。頻道、事件與授權端點的用法和 Pusher 完全相同。 免費建立帳號 →