Gói chính thức · Framework

Thời gian thực cho react-admin: danh sách tự cập nhật, người đang xem và khóa chỉnh sửa

Miễn phí và mã nguồn mở: danh sách và bản ghi tự cập nhật, bạn thấy ai khác đang mở cùng bản ghi, và mỗi lúc chỉ một người được sửa. react-admin 5, React 18 và 19, mọi data provider.

Phiên bản trên npmGiấy phép

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 react-admin (@hibira-io/react-admin) to this project.
    Follow https://hibira.io/guides/react-admin, 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.
  • <LiveRefresh /> tải lại danh sách, bản ghi và dữ liệu tham chiếu khi có người thay đổi.
  • <RecordViewers /> cho thấy ai khác đang mở bản ghi, và <RecordLockGuard> giữ biểu mẫu ở chế độ chỉ đọc khi người khác đang sửa, không cần bảng khóa.
  • <LiveRecord /> báo cho người đang sửa khi bản ghi được lưu hoặc xóa ở nơi khác, và <NewRecordsBadge /> đếm bản ghi mới trên menu.
Hai người mở cùng một đơn hàng trong react-admin: người mở trước được sửa, người mở sau thấy ai đang sửa và nhận biểu mẫu chỉ đọc.
Hai người mở cùng một đơn hàng trong react-admin: người mở trước được sửa, người mở sau thấy ai đang sửa và nhận biểu mẫu chỉ đọc.

Đă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 @hibira-io/react-admin pusher-js
    # on your backend
    npm install pusher
  2. Thiết lập

    import { Admin, Edit, List, Resource, SimpleForm } from 'react-admin';
    import Pusher from 'pusher-js';
    import { addRealtimeMethods, LiveRecord, LiveRefresh, RecordLockGuard, RecordViewers } from '@hibira-io/react-admin';
    
    const pusher = new Pusher(import.meta.env.VITE_HIBIRA_KEY, {
      cluster: 'hkg',
      wsHost: 'ws.hibira.io',
      channelAuthorization: { endpoint: '/api/hibira/auth', transport: 'ajax' },
    });
    const realtime = addRealtimeMethods(dataProvider, pusher, { channelPrefix: 'private-' });
    
    const OrderList = () => <List><LiveRefresh />{/* ... */}</List>;
    const OrderEdit = () => (
      <Edit actions={<RecordViewers />}>
        <LiveRecord />
        <RecordLockGuard><SimpleForm>{/* ... */}</SimpleForm></RecordLockGuard>
      </Edit>
    );
    
    export const App = () => (
      <Admin dataProvider={realtime}>
        <Resource name="orders" list={OrderList} edit={OrderEdit} />
      </Admin>
    );
  3. Sử dụng

    import Pusher from 'pusher';
    import { authorizeChannel, publishResourceEvent } from '@hibira-io/react-admin/server';
    
    const pusher = new Pusher({
      appId: process.env.HIBIRA_APP_ID!,
      key: process.env.HIBIRA_KEY!,
      secret: process.env.HIBIRA_SECRET!,
      host: 'ws.hibira.io',
      useTLS: true,
    });
    
    // POST /api/hibira/auth, after checking the session: signs private and presence channels
    authorizeChannel(pusher, socketId, channelName, { id: user.id, fullName: user.name });
    
    // after updating order 42
    await publishResourceEvent(pusher, { resource: 'orders', type: 'updated', ids: [42] }, { channelPrefix: 'private-', socketId: req.headers['x-socket-id'] });

Trước khi bắt đầu

  • Kênh private và presence cần một auth endpoint trên backend ký các lượt đăng ký bằng secret của ứng dụng, giống như với pusher-js.
  • Khóa chỉnh sửa thuộc về người mở bản ghi đầu tiên và chuyển sang người kế tiếp ngay khi họ rời đi. Nếu presence không dùng được, sẽ không khóa gì.

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