Gói chính thức · Framework

Cập nhật trực tiếp trong Qwik

Chỉ kết nối tới Hibira từ component Qwik khi nó hiển thị, để pusher-js không nằm trong lần tải đầu, và ký kênh riêng tư trong một endpoint Qwik City.

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 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$ tải pusher-js khi cần và ngắt kết nối khi cleanup.
  • Một endpoint onPost của Qwik City ký kênh riêng tư và presence bằng gói pusher trên npm.

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

    npm install pusher-js pusher
  2. Sử dụng

    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>;
    });
  3. Ký đăng ký kênh

    // 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));
    };

Trước khi bắt đầu

  • 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í.
  • 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.

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