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

Phoenix からリアルタイムイベントとチャンネル認可

Elixir から Hibira のイベントを送信し、Phoenix のコントローラーでプライベート・プレゼンスチャンネルの購読に署名します。追加のパッケージは不要です:HTTP API は HMAC-SHA256 で署名され、:crypto で行えます。

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

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

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

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

    Add Hibira Phoenix (Req) to this project.
    Follow https://hibira.io/guides/phoenix, 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.
  • ブラウザーやアプリは pusher-js や任意の Pusher クライアントで購読し、Phoenix は署名付きの POST 1 回で送信します。
  • 同じ HMAC でチャンネル購読と Live Chat の認証済み訪問者にも署名します。

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

  1. インストール

    # mix.exs
    {:req, "~> 0.5"}
  2. イベントを送信する

    defmodule MyApp.Hibira do
      @host "https://ws.hibira.io"
      defp env(k), do: System.fetch_env!(k)
      defp hmac(data), do: :crypto.mac(:hmac, :sha256, env("HIBIRA_SECRET"), data) |> Base.encode16(case: :lower)
    
      def trigger(channels, event, data) do
        path = "/apps/#{env("HIBIRA_APP_ID")}/events"
        body = Jason.encode!(%{name: event, channels: channels, data: Jason.encode!(data)})
        query = [
          auth_key: env("HIBIRA_KEY"),
          auth_timestamp: System.os_time(:second),
          auth_version: "1.0",
          body_md5: :crypto.hash(:md5, body) |> Base.encode16(case: :lower)
        ]
        signed = query |> Enum.sort() |> Enum.map_join("&", fn {k, v} -> "#{k}=#{v}" end)
        Req.post!(@host <> path, body: body, headers: [{"content-type", "application/json"}],
          params: query ++ [auth_signature: hmac("POST\n#{path}\n#{signed}")])
      end
    
      # The body of POST /pusher/auth, from socket_id and channel_name.
      def auth(socket_id, channel), do: %{auth: "#{env("HIBIRA_KEY")}:#{hmac("#{socket_id}:#{channel}")}"}
    end
  3. チャンネル購読に署名する

    # router.ex: post "/pusher/auth", PusherController, :auth
    def auth(conn, %{"socket_id" => socket_id, "channel_name" => "private-user-" <> id = channel}) do
      if to_string(conn.assigns.current_user.id) == id,
        do: json(conn, MyApp.Hibira.auth(socket_id, channel)),
        else: send_resp(conn, 403, "")
    end

始める前に

  • アプリのシークレットは、アプリの作成時とシークレットのローテーション時に一度だけ表示されます。サーバーにだけ置き、ブラウザやゲームのビルドには入れないでください。
  • プライベートチャンネルとプレゼンスチャンネルには、pusher-js と同じく、アプリのシークレットで購読に署名する認可エンドポイントがバックエンドに必要です。
  • プレゼンスチャンネルでは "socket_id:channel:channel_data" に署名し、Pusher プロトコルと同じく channel_data を auth と一緒に返します。

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