官方套件 · 框架

從 Hanami 發送即時事件

把 pusher gem 註冊為 Hanami 2 provider,從 action 與 operation 發送 Hibira 事件,並在 action 中簽署頻道訂閱。

最快的方式:裝好 Hibira 外掛,貼上一段提示詞

在你的 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.

想自己動手? 下方的步驟如下:

  1. 安裝
  2. 設定
  3. 使用

其他 AI 工具與外掛內容 →

  • provider 註冊一個 client,action 與 operation 透過 Deps 取得。
  • 由 action 為 pusher-js 與 Laravel Echo 簽署私有與 presence 頻道。

登入後,範例程式碼會自動帶入您的叢集主機。 登入

  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,之後是您的網域)。"*" 需要付費方案。

請使用儀表板中的應用程式 Key;Secret 只放在你的伺服器上。頻道、事件與授權端點的用法和 Pusher 完全相同。 免費建立帳號 →