官方套件 · 框架

在 SolidJS 以 signal 接收即時更新

在 SolidJS 用 pusher-js 訂閱 Hibira 頻道,再用一個小 primitive 把事件變成 signal,並隨擁有者一起清除。私有頻道由 SolidStart 在伺服器端簽署。

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

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

  1. 安裝外掛

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

    Add Hibira SolidJS (pusher-js) to this project.
    Follow https://hibira.io/guides/solidjs, 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.
  • createEvent 回傳保存最新事件的 signal,元件卸載時自動取消綁定。
  • SolidStart API 路由以 pusher npm 套件簽署私有與 presence 頻道。

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

  1. 安裝

    npm install pusher-js pusher
  2. 使用

    import Pusher from 'pusher-js';
    import { createSignal, onCleanup } from 'solid-js';
    
    const pusher = new Pusher(import.meta.env.VITE_HIBIRA_APP_KEY, {
      cluster: 'hkg',
      wsHost: 'ws.hibira.io',
      channelAuthorization: { endpoint: '/api/hibira/auth', transport: 'ajax' },
    });
    
    export function createEvent<T>(channelName: string, event: string) {
      const [value, setValue] = createSignal<T>();
      const channel = pusher.subscribe(channelName);
      const handler = (data: T) => setValue(() => data);
      channel.bind(event, handler);
      onCleanup(() => channel.unbind(event, handler));
      return value;
    }
    
    // const order = createEvent<{ id: number }>('orders', 'order.shipped');
  3. 簽署頻道訂閱

    // src/routes/api/hibira/auth.ts (SolidStart)
    import Pusher from 'pusher';
    
    const pusher = new Pusher({
      appId: process.env.HIBIRA_APP_ID!, key: process.env.HIBIRA_APP_KEY!, secret: process.env.HIBIRA_APP_SECRET!,
      host: 'ws.hibira.io', port: '443', useTLS: true,
    });
    
    export async function POST({ request }: { request: Request }) {
      const form = await request.formData();
      // check the signed-in user may join this channel first
      return Response.json(pusher.authorizeChannel(String(form.get('socket_id')), String(form.get('channel_name'))));
    }

開始之前

  • Free 方案需要把頁面載入的每個來源都加進應用程式的「允許的來源」(開發時是 http://localhost:3000,之後是您的網域)。"*" 需要付費方案。
  • 應用程式的密鑰只會在建立應用程式或輪替密鑰時顯示一次。請只放在伺服器上,不要放進瀏覽器或遊戲的建置檔。

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