公式パッケージ · フレームワーク

Qwik でライブ更新

Qwik のコンポーネントが表示されてから Hibira に接続するので、pusher-js は初回の読み込みに含まれません。プライベートチャンネルは Qwik City のエンドポイントで署名します。

最速の方法:Hibira プラグインとプロンプト 1 つ

AI コーディングエージェントにプラグインを入れて、プロンプトを貼り付けるだけ。エージェントがサインインし、必要なら無料アプリを作成し、コードを書いてテストイベントを送ります。 GitHub ↗

  1. プラグインをインストール

    claude plugin marketplace add hibira-io/agent-plugin
    claude plugin install hibira@hibira
  2. このプロンプトを貼り付け

    Add Hibira Qwik (pusher-js) to this project.
    Follow https://hibira.io/guides/qwik, 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.
  • useVisibleTask$ は pusher-js を必要なときに読み込み、cleanup で切断します。
  • Qwik City の onPost エンドポイントが pusher npm パッケージでプライベート・プレゼンスチャンネルに署名します。

サインインすると、コード例にご自身のクラスターホストが入ります。 サインイン

  1. インストール

    npm install pusher-js pusher
  2. 使う

    import { component$, useSignal, useVisibleTask$ } from '@builder.io/qwik';
    
    export default component$(() => {
      const status = useSignal('Waiting for updates');
      useVisibleTask$(async ({ cleanup }) => {
        const { default: Pusher } = await import('pusher-js');
        const pusher = new Pusher(import.meta.env.PUBLIC_HIBIRA_APP_KEY, {
          cluster: 'hkg',
          wsHost: 'ws.hibira.io',
          channelAuthorization: { endpoint: '/api/hibira/auth', transport: 'ajax' },
        });
        pusher.subscribe('orders').bind('order.shipped', (o: { id: number }) => (status.value = `Order #${o.id} shipped`));
        cleanup(() => pusher.disconnect());
      });
      return <p>{status.value}</p>;
    });
  3. チャンネル購読に署名する

    // src/routes/api/hibira/auth/index.ts
    import type { RequestHandler } from '@builder.io/qwik-city';
    import Pusher from 'pusher';
    
    export const onPost: RequestHandler = async ({ parseBody, env, json }) => {
      const pusher = new Pusher({
        appId: env.get('HIBIRA_APP_ID')!, key: env.get('HIBIRA_APP_KEY')!, secret: env.get('HIBIRA_APP_SECRET')!,
        host: 'ws.hibira.io', port: '443', useTLS: true,
      });
      const form = (await parseBody()) as Record<string, string>;
      // check the signed-in user may join this channel first
      json(200, pusher.authorizeChannel(form.socket_id, form.channel_name));
    };

始める前に

  • Free プランでは、ページを読み込むすべてのオリジンをアプリの許可オリジンに登録してください(開発中は http://localhost:3000、その後は自分のドメイン)。"*" は有料プランで使えます。
  • アプリのシークレットは、アプリの作成時とシークレットのローテーション時に一度だけ表示されます。サーバーにだけ置き、ブラウザやゲームのビルドには入れないでください。

ダッシュボードのアプリキーを使ってください。シークレットはサーバー側だけに置きます。チャンネル、イベント、認可エンドポイントは Pusher とまったく同じように動作します。 無料でアカウントを作成 →