# Refine 管理画面のリアルタイム更新

> Refine の liveProvider：誰かがデータを変えると、一覧・詳細・ダッシュボードが自動で更新されます。Refine 4 と 5、どの UI キットやデータプロバイダーでも使えます。

Source: https://hibira.io/ja/guides/refine

Refine の liveProvider：誰かがデータを変えると、一覧・詳細・ダッシュボードが自動で更新されます。Refine 4 と 5、どの UI キットやデータプロバイダーでも使えます。

Package: @hibira-io/refine

- liveMode: "auto" にすると、useList、useOne、useTable、useForm、useShow が自動で再取得します。
- 自前の認証エンドポイントを使う private チャンネル。同じリソースのフックは 1 つの購読を共有します。
- publishLiveEvent() は変更のたびに API からイベントを送り、変更したタブを除外することもできます。

## 1. インストール

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

## 2. 設定する

```tsx
import { Refine } from '@refinedev/core';
import Pusher from 'pusher-js';
import { liveProvider } from '@hibira-io/refine';

const pusher = new Pusher(import.meta.env.VITE_HIBIRA_KEY, {
  cluster: 'hkg',
  wsHost: 'ws.hibira.io',
  channelAuthorization: { endpoint: '/api/hibira/auth', transport: 'ajax' },
});

export const App = () => (
  <Refine
    dataProvider={dataProvider}
    liveProvider={liveProvider(pusher, { channelPrefix: 'private-', publish: false })}
    options={{ liveMode: 'auto' }}
  />
);
```

## 3. 使う

```ts
import Pusher from 'pusher';
import { publishLiveEvent } from '@hibira-io/refine/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,
});

// after updating post 42
await publishLiveEvent(pusher, { resource: 'posts', type: 'updated', ids: [42] }, { channelPrefix: 'private-', socketId: req.headers['x-socket-id'] });
```

## 始める前に

- プライベートチャンネルとプレゼンスチャンネルには、pusher-js と同じく、アプリのシークレットで購読に署名する認可エンドポイントがバックエンドに必要です。
- Laravel からも同じイベントを送れます。resources.posts に「refine」を、Refine の LiveEvent と同じ形の本文でブロードキャストします。

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