官方套件 · 框架
在 Qwik 接收即時更新
Qwik 元件在可見時才連線到 Hibira,讓 pusher-js 不進入首次載入;私有頻道在 Qwik City endpoint 簽署。
最快的方式:裝好 Hibira 外掛,貼上一段提示詞
在你的 AI 程式助手裝好外掛,再貼上提示詞。助手會幫你登入、需要時建立免費應用、寫好程式碼並送出測試事件。 GitHub ↗
安裝外掛
claude plugin marketplace add hibira-io/agent-plugin claude plugin install hibira@hibira貼上這段提示詞
Add Hibira Qwik (pusher-js) to this project. Follow https://hibira.io/guides/qwik, 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.
- useVisibleTask$ 按需載入 pusher-js,並在清除時中斷連線。
- Qwik City 的 onPost endpoint 以 pusher npm 套件簽署私有與 presence 頻道。
登入後,範例程式碼會自動帶入您的叢集主機。 登入
安裝
npm install pusher-js pusher使用
import { component$, useSignal, useVisibleTask$ } from '@builder.io/qwik'; export default component$(() => { const status = useSignal('Waiting for updates'); useVisibleTask$(async ({ cleanup }) => { const { default: Pusher } = await import('pusher-js'); const pusher = new Pusher(import.meta.env.PUBLIC_HIBIRA_APP_KEY, { cluster: 'hkg', wsHost: 'ws.hibira.io', channelAuthorization: { endpoint: '/api/hibira/auth', transport: 'ajax' }, }); pusher.subscribe('orders').bind('order.shipped', (o: { id: number }) => (status.value = `Order #${o.id} shipped`)); cleanup(() => pusher.disconnect()); }); return <p>{status.value}</p>; });簽署頻道訂閱
// src/routes/api/hibira/auth/index.ts import type { RequestHandler } from '@builder.io/qwik-city'; import Pusher from 'pusher'; export const onPost: RequestHandler = async ({ parseBody, env, json }) => { const pusher = new Pusher({ appId: env.get('HIBIRA_APP_ID')!, key: env.get('HIBIRA_APP_KEY')!, secret: env.get('HIBIRA_APP_SECRET')!, host: 'ws.hibira.io', port: '443', useTLS: true, }); const form = (await parseBody()) as Record<string, string>; // check the signed-in user may join this channel first json(200, pusher.authorizeChannel(form.socket_id, form.channel_name)); };
開始之前
- Free 方案需要把頁面載入的每個來源都加進應用程式的「允許的來源」(開發時是 http://localhost:3000,之後是您的網域)。"*" 需要付費方案。
- 應用程式的密鑰只會在建立應用程式或輪替密鑰時顯示一次。請只放在伺服器上,不要放進瀏覽器或遊戲的建置檔。
請使用儀表板中的應用程式 Key;Secret 只放在你的伺服器上。頻道、事件與授權端點的用法和 Pusher 完全相同。 免費建立帳號 →