官方包 · 框架

用 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 完全相同。 免费创建账号 →