官方包 · 框架

react-admin 实时功能:列表自动更新、谁在查看、编辑锁

免费开源:列表和记录会自动更新,能看到还有谁打开了同一条记录,并且同一时间只有一个人能编辑。支持 react-admin 5、React 18 和 19,以及任何 data provider。

npm 版本许可证

最快的方式:装好 Hibira 插件,粘贴一段提示词

在你的 AI 编程助手装好插件,再粘贴提示词。助手会帮你登录、需要时创建免费应用、写好代码并发送测试事件。 GitHub ↗

  1. 安装插件

    claude plugin marketplace add hibira-io/agent-plugin
    claude plugin install hibira@hibira
  2. 粘贴这段提示词

    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.

想自己动手? 下方的步骤如下:

  1. 安装
  2. 配置
  3. 使用

其他 AI 工具与插件内容 →

  • <LiveRefresh /> 在有人修改数据时重新获取列表、记录和关联数据。
  • <RecordViewers /> 显示还有谁打开了这条记录;<RecordLockGuard> 在别人编辑时让表单保持只读,无需锁表。
  • <LiveRecord /> 在记录被别人保存或删除时提醒正在编辑的人;<NewRecordsBadge /> 在菜单中显示新记录数量。
两个人在 react-admin 中打开同一个订单:先打开的人可以编辑,后来的人会看到谁在编辑,表单变为只读。
两个人在 react-admin 中打开同一个订单:先打开的人可以编辑,后来的人会看到谁在编辑,表单变为只读。

登录后,示例代码会自动填入您的集群主机。 登录

  1. 安装

    npm install @hibira-io/react-admin pusher-js
    # on your backend
    npm install pusher
  2. 配置

    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. 使用

    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'] });

开始之前

  • 私有频道和 presence 频道需要后端提供授权端点,用应用密钥签名订阅,做法与 pusher-js 相同。
  • 编辑锁属于最先打开记录的人,对方一离开就交给下一个人。presence 不可用时不会加锁。

请使用控制台中应用的 Key;Secret 只放在你的服务器上。频道、事件与授权端点的用法与 Pusher 完全相同。 免费创建账号 →