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

Hanami からリアルタイムイベント

pusher gem を Hanami 2 の provider として登録し、action や operation から Hibira のイベントを送信し、action でチャンネル購読に署名します。

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

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

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

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

    Add Hibira Hanami (pusher) to this project.
    Follow https://hibira.io/guides/hanami, 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.
  • provider がクライアントを 1 つ登録し、action や operation は Deps で受け取ります。
  • action が pusher-js と Laravel Echo のためにプライベート・プレゼンスチャンネルに署名します。

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

  1. インストール

    bundle add pusher
  2. 設定する

    # config/providers/hibira.rb
    Hanami.app.register_provider(:hibira) do
      prepare { require "pusher" }
      start do
        register "hibira", Pusher::Client.new(
          app_id: target["settings"].hibira_app_id, key: target["settings"].hibira_app_key,
          secret: target["settings"].hibira_app_secret, host: "ws.hibira.io", port: 443, use_tls: true
        )
      end
    end
    
    # config/settings.rb: setting :hibira_app_id, setting :hibira_app_key, setting :hibira_app_secret
  3. 使う

    # app/actions/pusher/auth.rb (config/routes.rb: post "/pusher/auth", to: "pusher.auth")
    module Bookshelf
      module Actions
        module Pusher
          class Auth < Bookshelf::Action
            include Deps["hibira"]
    
            def handle(request, response)
              # check the signed-in user may join request.params[:channel_name] first
              response.format = :json
              response.body = hibira.authenticate(request.params[:channel_name], request.params[:socket_id]).to_json
            end
          end
        end
      end
    end
    
    # anywhere: hibira.trigger("orders", "order.shipped", { id: 1042 })

始める前に

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

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