Gói chính thức · Framework

Sự kiện thời gian thực và cấp quyền kênh từ Phoenix

Gửi sự kiện Hibira từ Elixir và ký đăng ký kênh riêng tư và presence trong một controller Phoenix. Không cần thêm gói: HTTP API được ký bằng HMAC-SHA256, việc mà :crypto làm được.

Cách nhanh nhất: plugin Hibira và một prompt

Cài plugin vào agent lập trình AI của bạn rồi dán prompt. Agent sẽ đăng nhập, tạo ứng dụng miễn phí nếu cần, viết mã và gửi một sự kiện thử. GitHub ↗

  1. Cài plugin

    claude plugin marketplace add hibira-io/agent-plugin
    claude plugin install hibira@hibira
  2. Dán prompt này

    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.
  • Trình duyệt và ứng dụng đăng ký bằng pusher-js hoặc bất kỳ client Pusher nào; Phoenix gửi bằng một lệnh POST có chữ ký.
  • Cùng một HMAC ký đăng ký kênh và khách truy cập đã xác minh trong Live Chat.

Đăng nhập để xem các đoạn mã này với host cụm của riêng bạn. Đăng nhập

  1. Cài đặt

    # mix.exs
    {:req, "~> 0.5"}
  2. Gửi sự kiện

    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. Ký đăng ký kênh

    # 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

Trước khi bắt đầu

  • Secret của ứng dụng chỉ hiện một lần, khi bạn tạo ứng dụng hoặc xoay vòng secret. Hãy giữ nó trên máy chủ, không bao giờ đưa vào trình duyệt hay bản build game.
  • Kênh private và presence cần một auth endpoint trên backend ký các lượt đăng ký bằng secret của ứng dụng, giống như với pusher-js.
  • Với kênh presence, hãy ký "socket_id:channel:channel_data" và trả channel_data cùng auth, như trong giao thức Pusher.

Dùng app key từ bảng điều khiển; secret chỉ để trên máy chủ của bạn. Kênh, sự kiện và endpoint xác thực hoạt động y như với Pusher. Tạo tài khoản miễn phí →