このガイドでは、カスタムプラグインを作成し、Cloudflare RealtimeKit Core SDK を使ってミーティング内で実行する方法を説明します。
カスタムプラグインは、SDK に登録する DOM 要素です。参加者がプラグインを有効にすると、RealtimeKit はセッション内の全員に対してアクティブにし、ミーティングレイアウト内に描画します。
This page is not available for the Mobile platform.
Web Mobile
React Web Components Angular
このページは SDK の初期化 と プラグイン のガイドを前提にしています。先にそちらを読んでください。
例では、必要なパッケージのインポートと SDK の初期化が済んでいる前提です。
プラグインは次の 2 つの部分で構成されます。
コンポーネント : プラグインの UI とロジックを持つ DOM 要素(HTMLElement)です。
登録 : SDK に渡す設定オブジェクトです。SDK はこの情報でコンポーネントを一覧表示、有効化、描画します。
RealtimeKit は、有効化の状態をセッション全体で同期します。参加者間でプラグインのデータを共有するには、collaborative stores を使います。
プラグインコンポーネントは HTMLElement である必要があります。カスタム要素として直接作成するか、コンテナ要素を作ってフレームワークのコンポーネントツリーをマウントします。
プラグインをカスタム要素として定義し、component として渡すインスタンスを作成します。
my-counter-plugin.ts ts class MyCounterPlugin extends HTMLElement {
connectedCallback () {
this .innerHTML = `
<div class="counter">
<button id="increment">This is a counter plugin</button>
<span id="value">0</span>
</div>
` ;
}
}
customElements. define ( "my-counter-plugin" , MyCounterPlugin);
const pluginElement = document. createElement ( "my-counter-plugin" );
コンテナ要素を作成し、その中にコンポーネントツリーをマウントします。コンテナを component として渡します。
counter-plugin.tsx tsx import { createRoot } from "react-dom/client" ;
function CounterPlugin () {
return < div className = "counter" >This is a counter plugin</ div >;
}
const pluginElement = document. createElement ( "div" );
createRoot (pluginElement). render (< CounterPlugin />);
コンテナ要素を作成し、その中にコンポーネントツリーをマウントします。コンテナを component として渡します。
counter-plugin.ts ts import {
createComponent,
ApplicationRef,
EnvironmentInjector,
} from "@angular/core" ;
import { CounterPluginComponent } from "./counter-plugin.component" ;
const pluginElement = document. createElement ( "div" );
const componentRef = createComponent (CounterPluginComponent, {
environmentInjector: this .injector,
hostElement: pluginElement,
});
this .appRef. attachView (componentRef.hostView);
SDK を初期化するときに、セッションで利用できるプラグインを登録します。プラグイン設定の配列を defaults.plugins として渡し、手順 1 で作成した pluginElement を component に指定します。
RealtimeKitClient. init ({
authToken: "<auth_token>" ,
defaults: {
plugins: [
{
id: "counter" ,
name: "Counter" ,
icon: "https://example.com/counter.png" ,
permissions: {
canActivate: true ,
canDeactivate: true ,
},
component: pluginElement,
},
],
},
});
各設定フィールドの説明は、プラグインを登録する を参照してください。
登録後、プラグインは meeting.plugins.all に現れます。有効化すると、セッション内の全員に対してアクティブになります。
const plugin = meeting.plugins.all. get (pluginId);
await plugin. activate ();
const plugins = useRealtimeKitSelector (( m ) => m.plugins);
const plugin = plugins.all. get (pluginId);
await plugin. activate ();
Note
UI Kit を使う場合、プラグインコンポーネントが有効化と描画を処理します。
Plugin オブジェクトは、状態が変わるときにイベントを発行します。有効化や無効化のタイミングで、コンポーネントのセットアップや破棄に使います。
const plugin = meeting.plugins.all. get (pluginId);
plugin. on ( "enabled" , () => {
// The plugin became active for the local participant
});
plugin. on ( "closed" , () => {
// The plugin was deactivated for the local participant
});
プラグインイベントの一覧は、プラグインイベントを購読する を参照してください。
各参加者はプラグインコンポーネントのコピーをそれぞれ実行します。状態を共有する仕組みが必要です。RealtimeKit には、リアルタイム通信向けの組み込みオプションが 2 つあります。
要件が単純なプラグインでは、これらの組み込み API で共同作業のロジックをまかなえます。
// Create or get a store for your plugin
const store = meeting.stores. create ( "counter" );
// Update a value for all participants
await store. set ( "value" , 1 );
// React to updates from any participant
store. subscribe ( "value" , ({ value }) => {
document. querySelector ( "#value" ).textContent = value;
});
より高機能な共同作業が必要な場合は、専用のサードパーティフレームワークと組み合わせられます。