This library runs inside a custom plugin iframe and talks to the host over window.postMessage. It supports JSON-RPC 2.0 (recommended) and a legacy envelope format (type on the message object) for older hosts.
-
Import the client (default export is
CustomPluginClient):import CustomPluginClient from "@oviceinc/custom-plugin-client";
-
Create an instance and call
ready():const client = new CustomPluginClient(); const disconnect = client.ready({ expectedOrigin: "https://example.com" });
-
Register event listeners with
on:client.on("message", (payload) => { // Handle message event }); client.on("participants", (payload) => { // Handle participants event }); client.on("data_saved", (payload) => { // Host confirmed save });
-
Call void methods (JSON-RPC requests) or async RPC helpers:
client.getParticipants(); void client.getParticipantsAsync().then((list) => { console.log("RPC result", list); }); client.sendMessage({ to: "participantId", event: "eventName", message: "Hello, participant!", }); client.broadcast({ event: "eventName", message: "Hello, everyone!", }); client.saveData({ key: "value" });
-
Remove listeners with
offorDisposable.off()fromon, or useaddEventListenersfor a batch:client.off("message", messageHandler); const removable = client.addEventListeners({ message: messageHandler, participants: participantsHandler, }); removable.removeEventListeners();
Replace https://example.com with the parent window origin you expect.
If the host only speaks legacy postMessage objects with a top-level type field (not JSON-RPC), use LegacyCustomPluginClient. It is deprecated; prefer migrating the host to JSON-RPC and using CustomPluginClient.
import { LegacyCustomPluginClient } from "@oviceinc/custom-plugin-client";
const client = new LegacyCustomPluginClient();
client.ready({ expectedOrigin: "https://example.com" });On the legacy client, getParticipantsAsync, sendMessageAsync, broadcastAsync, and saveDataAsync throw—they require JSON-RPC. Only expectedOrigin from ready() options is used; rpcTimeoutMs is ignored.
CustomPluginClient uses JSON-RPC 2.0 for outbound requests and inbound responses, and JSON-RPC notifications from the host (see table below).
| Option | Description |
|---|---|
expectedOrigin |
Strongly recommended: validates inbound MessageEvent.origin and sets the target origin for postMessage. |
rpcTimeoutMs |
Timeout in ms for JSON-RPC requests (default 30000). |
JSON-RPC method uses the same string as the legacy message type (e.g. ovice_participants).
method |
Client event |
|---|---|
ovice_participants |
participants |
ovice_message |
message |
ovice_participant_joined |
joined |
ovice_participant_left |
left |
ovice_participant_subscribed |
subscribed |
ovice_participant_unsubscribed |
unsubscribed |
ovice_shared_data |
shared_data |
ovice_data_saved_success |
data_saved |
ovice_errors |
errors |
Notifications must omit id (JSON-RPC notification). Batches are accepted as a JSON array of objects; each element is handled independently.
JSON-RPC method matches the legacy postMessage type for the same action.
method |
params |
Typical result |
|---|---|---|
ovice_get_participants |
{} (optional) |
Participant[] |
ovice_emit_message |
Same shape as sendMessage / broadcast payloads |
host-defined |
ovice_save_and_emit_data |
{ payload: object } (mirrors legacy save envelope) |
host-defined |
Errors use the standard JSON-RPC error object; async methods reject with JsonRpcError.
getParticipantsAsync, sendMessageAsync, broadcastAsync, and saveDataAsync send JSON-RPC and return a Promise. They are only available on CustomPluginClient.
JSON-RPC transport end-to-end. Default package export.
Legacy type-only envelopes. Use CustomPluginClient for new work.
Initializes the client and returns a teardown function that removes the message listener and clears pending JSON-RPC state (on CustomPluginClient).
- CustomPluginClient:
options—CustomPluginClientReadyOptions: optionalexpectedOrigin,rpcTimeoutMs. - LegacyCustomPluginClient: same shape; only
expectedOriginis used.
Registers a callback for the given event.
event:participants,message,joined,left,subscribed,unsubscribed,shared_data,data_saved, orerrors.callback: Handler invoked with the event payload.
Requests the participant list (JSON-RPC on CustomPluginClient, legacy envelope on LegacyCustomPluginClient).
JSON-RPC request; resolves with the RPC result. Not supported on LegacyCustomPluginClient.
Sends a message to one participant (to, event, message).
JSON-RPC variant of sendMessage.
Broadcasts an event / message to all participants.
JSON-RPC variant of broadcast.
Saves data via the plugin host.
JSON-RPC variant of saveData.
CustomPluginClientis now JSON-RPC-only; there is noprotocol/jsonRpcOutbound/"auto"switch onready().- If you relied on the previous default (legacy when
protocolwas omitted), useLegacyCustomPluginClientuntil the host supports JSON-RPC, then switch toCustomPluginClient. - Legacy
ovice_data_saved_successmaps to thedata_savedevent (notmessage).
- Default:
CustomPluginClient - Named:
LegacyCustomPluginClient,JsonRpcError,JsonRpcOutboundMethods,JsonRpcNotificationMethods,JsonRpcProtocol, and TypeScript types such asCustomPluginClientReadyOptions,LegacyCustomPluginClientReadyOptions,Participant, and payload types for host implementers.