官方套件 · 框架

用 Nest 的方式為 NestJS 加上即時事件

一個全域模組:發布事件、用加了裝飾器的 provider 方法授權私人與 presence 頻道,並接收簽章過的 Webhook。pusher-js、Laravel Echo 與行動版 Pusher SDK 不用改就能連線。

  • HibiraService 發布事件、查詢頻道與在線成員,也能傳送給已登入的使用者。
  • @HibiraChannel() 像 Laravel 的 routes/channels.php 一樣授權頻道,@Broadcast() 發布路由回傳的結果,@OnHibiraWebhook() 在任何 provider 裡接收 Webhook。
  • stream() 把 LLM 輸出送到頻道,requestApproval() 在程式動作前先請人核准,HibiraPushService 傳送推播通知。支援 Nest 10 到 12,Express 或 Fastify 皆可。

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

  1. 安裝

    npm install @hibira-io/nestjs
    
    # .env
    HIBIRA_APP_ID=YOUR_APP_ID
    HIBIRA_KEY=YOUR_APP_KEY
    HIBIRA_SECRET=YOUR_APP_SECRET
    HIBIRA_HOST=ws.hibira.io
  2. 設定

    // app.module.ts
    import { Module } from '@nestjs/common';
    import { HibiraModule } from '@hibira-io/nestjs';
    import { JwtAuthGuard } from './auth/jwt-auth.guard';
    
    @Module({
      imports: [
        HibiraModule.forRoot({
          auth: { guards: [JwtAuthGuard] }, // POST /hibira/auth
          webhooks: true, // POST /hibira/webhook
        }),
      ],
    })
    export class AppModule {}
  3. 使用

    @Injectable()
    export class OrdersService {
      constructor(private readonly hibira: HibiraService) {}
    
      async ship(order: Order) {
        await this.hibira.trigger(`private-orders.${order.id}`, 'order.shipped', { id: order.id });
      }
    
      @HibiraChannel('private-orders.{orderId}')
      async canSeeOrder(user: User, { orderId }: { orderId: string }) {
        return (await this.find(orderId))?.customerId === user.id;
      }
    }

開始之前

  • 應用程式的密鑰只會在建立應用程式或輪替密鑰時顯示一次。請只放在伺服器上,不要放進瀏覽器或遊戲的建置檔。
  • 要接收 Webhook,請在 main.ts 以 rawBody: true 建立應用程式,簽章才會用收到的原始位元組來驗證。

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