แพ็กเกจอย่างเป็นทางการ · เฟรมเวิร์ก

เหตุการณ์เรียลไทม์และการอนุญาตช่องจาก Phoenix

ส่งเหตุการณ์ Hibira จาก Elixir และลงนามการสมัครช่องส่วนตัวและ presence ใน controller ของ Phoenix ไม่ต้องเพิ่มแพ็กเกจ: HTTP API ลงนามด้วย HMAC-SHA256 ซึ่ง :crypto ทำได้

วิธีที่เร็วที่สุด: ปลั๊กอิน Hibira กับพรอมต์เดียว

ติดตั้งปลั๊กอินในเอเจนต์เขียนโค้ด 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 ที่ลงนามเพียงครั้งเดียว
  • 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

ก่อนเริ่ม

  • ซีเคร็ตของแอปจะแสดงเพียงครั้งเดียว ตอนสร้างแอปหรือตอนหมุนเวียนซีเคร็ต เก็บไว้บนเซิร์ฟเวอร์เท่านั้น ห้ามใส่ในเบราว์เซอร์หรือไฟล์บิลด์ของเกม
  • แชนเนล private และ presence ต้องมี auth endpoint บนแบ็กเอนด์ที่ลงนามการสมัครรับด้วยซีเคร็ตของแอป เหมือนกับ pusher-js
  • สำหรับช่อง presence ให้ลงนาม "socket_id:channel:channel_data" และส่ง channel_data กลับพร้อม auth ตามโปรโตคอล Pusher

ใช้ app key จากแดชบอร์ดของคุณ ส่วน secret ให้เก็บไว้บนเซิร์ฟเวอร์เท่านั้น ช่อง อีเวนต์ และ endpoint การยืนยันสิทธิ์ทำงานเหมือนกับที่ใช้กับ Pusher ทุกประการ สร้างบัญชีฟรีของคุณ →