官方套件 · 框架
從 Sinatra 發送即時事件
在 Sinatra 應用中,用指向你 Hibira 主機的 pusher gem 發送 Hibira 事件、簽署頻道訂閱並驗證 webhook。
pusherRubyGems ↗
最快的方式:裝好 Hibira 外掛,貼上一段提示詞
在你的 AI 程式助手裝好外掛,再貼上提示詞。助手會幫你登入、需要時建立免費應用、寫好程式碼並送出測試事件。 GitHub ↗
安裝外掛
claude plugin marketplace add hibira-io/agent-plugin claude plugin install hibira@hibira貼上這段提示詞
Add Hibira Sinatra (pusher) to this project. Follow https://hibira.io/guides/sinatra, 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 gem 可發送事件,並簽署私有與 presence 頻道。
- Webhook 會先以 App secret 驗證,再執行你的程式碼。
登入後,範例程式碼會自動帶入您的叢集主機。 登入
安裝
bundle add pusher使用
require "sinatra" require "pusher" HIBIRA = Pusher::Client.new( app_id: ENV.fetch("HIBIRA_APP_ID"), key: ENV.fetch("HIBIRA_APP_KEY"), secret: ENV.fetch("HIBIRA_APP_SECRET"), host: "ws.hibira.io", port: 443, use_tls: true ) post "/orders/:id/ship" do HIBIRA.trigger("orders", "order.shipped", { id: params[:id].to_i }) 204 end post "/pusher/auth" do halt 403 unless session[:user_id] # check the user may join params[:channel_name] content_type :json HIBIRA.authenticate(params[:channel_name], params[:socket_id]).to_json end post "/hibira/webhooks" do webhook = Pusher::WebHook.new(request, HIBIRA) halt 401 unless webhook.valid? webhook.events.each { |event| logger.info(event["name"]) } 200 end
開始之前
- 應用程式的密鑰只會在建立應用程式或輪替密鑰時顯示一次。請只放在伺服器上,不要放進瀏覽器或遊戲的建置檔。
- Free 方案需要把頁面載入的每個來源都加進應用程式的「允許的來源」(開發時是 http://localhost:3000,之後是您的網域)。"*" 需要付費方案。
請使用儀表板中的應用程式 Key;Secret 只放在你的伺服器上。頻道、事件與授權端點的用法和 Pusher 完全相同。 免費建立帳號 →