官方套件 · 框架

用 Spring 的方式為 Spring Boot 加上即時功能

一個 starter:注入 Hibira 用戶端,用加上註解的 bean 方法授權 private 與 presence 頻道,並把 Webhook 轉成應用程式事件。瀏覽器和 App 用 pusher-js、Laravel Echo 或 Pusher 行動 SDK 連線,不必自己架 WebSocket 伺服器或 STOMP broker。

  • @HibiraChannel 以 Spring Security 的使用者授權頻道,starter 也提供 pusher-js 呼叫的驗證端點,Spring MVC 與 WebFlux 皆可。
  • @HibiraBroadcast 會在交易提交後把應用程式事件送到瀏覽器,Webhook 則以 WebhookEvent 交給 @EventListener。
  • 用戶端也能寫入通知中心、發送推播與請求人工審核。支援 Spring Boot 3 與 4、Java 17 以上;不用 Spring 時可單獨使用 hibira-java。

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

  1. 安裝

    <dependency>
      <groupId>io.hibira</groupId>
      <artifactId>hibira-spring-boot-starter</artifactId>
      <version>0.1.0</version>
    </dependency>
    
    <!-- environment -->
    HIBIRA_APP_ID=YOUR_APP_ID
    HIBIRA_KEY=YOUR_APP_KEY
    HIBIRA_SECRET=YOUR_APP_SECRET
    HIBIRA_HOST=ws.hibira.io
  2. 設定

    @Component
    class Channels {
        @HibiraChannel("private-orders.{orderId}")
        boolean order(Principal user, long orderId) {
            return orders.isOwner(orderId, user.getName());
        }
    }
    
    @HibiraBroadcast(channels = "private-orders.#{orderId}", event = "order.shipped")
    record OrderShipped(long orderId, String carrier) {}
    
    // in a @Transactional service: sent once the transaction commits
    events.publishEvent(new OrderShipped(1042, "DHL"));
    hibira.trigger("orders", "order.created", Map.of("id", 1042)); // or send directly
  3. 使用

    const pusher = new Pusher('YOUR_APP_KEY', {
      wsHost: 'ws.hibira.io',
      cluster: '',
      enabledTransports: ['ws', 'wss'],
      channelAuthorization: { endpoint: '/hibira/auth', headers: { 'X-XSRF-TOKEN': csrfToken } },
      userAuthentication: { endpoint: '/hibira/user-auth', headers: { 'X-XSRF-TOKEN': csrfToken } },
    });
    pusher.signin(); // hibira.sendToUser(...) reaches this tab
    pusher.subscribe('private-orders.1042').bind('order.shipped', (order) => console.log(order));

開始之前

  • 應用程式的密鑰只會在建立應用程式或輪替密鑰時顯示一次。請只放在伺服器上,不要放進瀏覽器或遊戲的建置檔。
  • 使用 Spring Security 時,請從瀏覽器送出 CSRF token(或將 /hibira/auth 與 /hibira/user-auth 排除),並讓 /hibira/webhooks 不需工作階段與 CSRF token 即可存取:它會自行驗證簽章。
  • Free 方案請把頁面載入的所有來源加入應用程式的允許來源(開發時為 http://localhost:8080,之後是你的網域)。"*" 需要付費方案。

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