Gói chính thức · Framework

Sự kiện thời gian thực từ Hanami

Đăng ký gem pusher làm provider của Hanami 2, gửi sự kiện Hibira từ action và operation, và ký đăng ký kênh trong một action.

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 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.
  • Một provider đăng ký một client; action và operation lấy nó qua Deps.
  • Một action ký kênh riêng tư và presence cho pusher-js và Laravel Echo.

Đă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

    bundle add pusher
  2. Thiết lập

    # 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. Sử dụng

    # 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 })

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.
  • Với gói Free, hãy thêm mọi origin mà trang của bạn tải từ đó vào Origin được phép của ứng dụng (http://localhost:3000 khi phát triển, sau đó là tên miền của bạn). "*" cần gói trả phí.

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í →