官方套件 · 框架

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 完全相同。 免費建立帳號 →