kmo-plugin-sdk
v1.2.2
Published
SDK for plugins of the Kocka Másik Oldala webpage
Downloads
108
Readme
kmo-plugin-sdk
The kmo-plugin-sdk provides a simple interface for plugin developers to interact with the KMO platform. It supports initialization, role management, data persistence, and real-time messaging via WebSockets.
Installation
npm install kmo-plugin-sdkor if using yarn:
yarn add kmo-plugin-sdkUsage
import KMOPluginClient from "kmo-plugin-sdk";
// Initialize the plugin client
KMOPluginClient.init();
// Get the current user role
const role = KMOPluginClient.getCurrentRole();
// Data operations
await KMOPluginClient.data.save("myKey", { foo: "bar" });
const value = await KMOPluginClient.data.get("myKey");
await KMOPluginClient.data.remove("myKey");
// Messaging operations
KMOPluginClient.messaging.connect();
// Subscribe to incoming event and handle the response message
const unsubscribe = KMOPluginClient.messaging.subscribe((msg) => {
console.log("Received message:", msg);
});
// Send messages or data for all subscribers
KMOPluginClient.messaging.broadcast({ defMessage: "Hello world" });
// Check connection status
if (KMOPluginClient.messaging.isConnected()) {
console.log("WebSocket is connected");
}
// Disconnect when done
KMOPluginClient.messaging.disconnect();
// Unsubscribe when no longer needed
unsubscribe();API Reference
Initialization
init(): Initializes the client.getCurrentRole(): Role: Returns the role of the current user.
Data API
data.get(key: string): Promise<object | null>– Retrieves data by key.data.save(key: string, value: object): Promise<void>– Saves data under the given key.data.remove(key: string): Promise<void>– Removes data for the given key.
Messaging API
messaging.connect(): Promise<void>– Connects to the WebSocket server.messaging.disconnect(): void– Disconnects from the WebSocket server.messaging.subscribe(handler: (msg: unknown) => void): () => void– Subscribes to incoming messages. Returns an unsubscribe function.messaging.broadcast(msg: unknown): void– Sends a command message.messaging.isConnected(): boolean– Checks if WebSocket is connected.
Types
KMOPluginClientData
export type KMOPluginClientData = {
get: (key: string) => Promise<object | null>,
save: (key: string, data: object) => Promise<void>,
remove: (key: string) => Promise<void>
}KMOPluginClientMessaging
export type KMOPluginClientMessaging = {
connect: () => WebSocket | undefined,
disconnect: () => void,
subscribe: (handler: (event: Event) => void) => () => void,
sendCommand: (command: PluginCommand) => void,
isConnected: () => boolean,
onStatusChange: (cb: (status: Status) => void) => () => void
}KMOPluginClientType
export type KMOPluginClientType = {
init: () => void,
getCurrentRole: () => Role,
data: KMOPluginClientData,
messaging: KMOPluginClientMessaging
}