公式パッケージ · フレームワーク

react-admin のリアルタイム機能:一覧の自動更新、閲覧中の人、編集ロック

無料のオープンソース:一覧とレコードが自動で更新され、同じレコードを開いている人がわかり、同時に編集できるのは 1 人だけになります。react-admin 5、React 18 と 19、どのデータプロバイダーでも使えます。

npm のバージョンライセンス

最速の方法:Hibira プラグインとプロンプト 1 つ

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.
  • <LiveRefresh /> は誰かがデータを変えると、一覧・レコード・参照を取り直します。
  • <RecordViewers /> はレコードを開いている他の人を表示し、<RecordLockGuard> は他の人が編集している間フォームを読み取り専用にします。ロック用のテーブルは不要です。
  • <LiveRecord /> はレコードが別の場所で保存・削除されたときに編集中の人へ知らせ、<NewRecordsBadge /> はメニューに新しいレコードの数を表示します。
react-admin で 2 人が同じ注文を開いた様子:先に開いた人は編集でき、後から開いた人には編集中の人が表示され、フォームは読み取り専用になります。
react-admin で 2 人が同じ注文を開いた様子:先に開いた人は編集でき、後から開いた人には編集中の人が表示され、フォームは読み取り専用になります。

サインインすると、コード例にご自身のクラスターホストが入ります。 サインイン

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

始める前に

  • プライベートチャンネルとプレゼンスチャンネルには、pusher-js と同じく、アプリのシークレットで購読に署名する認可エンドポイントがバックエンドに必要です。
  • 編集ロックは最初にレコードを開いた人が持ち、その人が離れるとすぐ次の人に移ります。presence が使えないときはロックしません。

ダッシュボードのアプリキーを使ってください。シークレットはサーバー側だけに置きます。チャンネル、イベント、認可エンドポイントは Pusher とまったく同じように動作します。 無料でアカウントを作成 →