# Realtime for react-admin: live lists, viewers and edit locks

> Free and open source: lists and records update by themselves, you see who else has a record open, and only one person at a time can edit it. react-admin 5, React 18 and 19, any data provider.

Source: https://hibira.io/guides/react-admin

Free and open source: lists and records update by themselves, you see who else has a record open, and only one person at a time can edit it. react-admin 5, React 18 and 19, any data provider.

Package: @hibira-io/react-admin

- <LiveRefresh /> refetches a list, its records and references when anyone changes them.
- <RecordViewers /> shows who else has the record open, and <RecordLockGuard> keeps the form read-only while someone else edits, with no lock table.
- <LiveRecord /> warns an editor when the record was saved or deleted elsewhere, and <NewRecordsBadge /> counts new records in the menu.

## 1. Install

```shell
npm install @hibira-io/react-admin pusher-js
# on your backend
npm install pusher
```

## 2. Set it up

```tsx
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. Use it

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

## Before you start

- Private and presence channels need an auth endpoint on your backend that signs subscriptions with the app secret, as with pusher-js.
- The edit lock goes to whoever opened the record first and passes on as soon as they leave. If presence is unavailable, nothing is locked.

- [npm](https://www.npmjs.com/package/@hibira-io/react-admin)
- [GitHub](https://github.com/hibira-io/react-admin)
