tauri-plugin-centrifugo-api
Version:
Tauri plugin for Centrifugo real-time communication using tokio-centrifuge
221 lines (220 loc) • 6.46 kB
TypeScript
import { type UnlistenFn } from '@tauri-apps/api/event';
export type { UnlistenFn };
export interface StartConfig {
url: string;
token?: string;
name?: string;
version?: string;
channels: string[];
useProtobuf: boolean;
readTimeout?: number;
}
export interface PublishRequest {
channel: string;
data: string;
}
export interface RpcRequest {
method: string;
data: string;
}
export interface PresenceRequest {
channel: string;
}
export interface PresenceStatsRequest {
channel: string;
}
export interface HistoryRequest {
channel: string;
limit?: number;
since?: string;
reverse?: boolean;
}
export interface SendRequest {
data: string;
}
export interface RefreshRequest {
token: string;
}
export interface SubRefreshRequest {
channel: string;
token: string;
}
export interface PublicationData {
channel: string;
data: string;
}
export interface ConnectionStatus {
ts: number;
}
export interface ChannelStatus {
channel: string;
}
export interface ErrorData {
error: string;
}
export interface TauriEventWrapper<T> {
event: string;
payload: T;
id: number;
}
export interface RpcResponse {
method: string;
data: string;
timestamp: number;
}
export interface PresenceResponse {
channel: string;
clients: string[];
timestamp: number;
}
export interface PresenceStatsResponse {
channel: string;
numClients: number;
numUsers: number;
timestamp: number;
}
export interface HistoryResponse {
channel: string;
publications: PublicationData[];
timestamp: number;
}
export interface PingResponse {
timestamp: number;
}
/**
* Connect to Centrifugo
*/
export declare function connect(config: StartConfig): Promise<void>;
/**
* Disconnect from Centrifugo
*/
export declare function disconnect(): Promise<void>;
/**
* Set authentication token
*/
export declare function setToken(token: string): Promise<void>;
/**
* Publish message to channel
*/
export declare function publish(request: PublishRequest): Promise<void>;
/**
* Execute RPC call
*/
export declare function rpc(request: RpcRequest): Promise<string>;
/**
* Get presence information for channel
*/
export declare function presence(request: PresenceRequest): Promise<string[]>;
/**
* Get presence statistics for channel
*/
export declare function presenceStats(request: PresenceStatsRequest): Promise<[number, number]>;
/**
* Get message history for channel
*/
export declare function history(request: HistoryRequest): Promise<PublicationData[]>;
/**
* Send message to server
*/
export declare function send(request: SendRequest): Promise<void>;
/**
* Refresh authentication token
*/
export declare function refresh(request: RefreshRequest): Promise<void>;
/**
* Refresh subscription token
*/
export declare function subRefresh(request: SubRefreshRequest): Promise<void>;
/**
* Ping server
*/
export declare function ping(): Promise<void>;
/**
* Check if connected to Centrifugo
*/
export declare function isConnected(): Promise<boolean>;
/**
* Get current subscriptions
*/
export declare function getSubscriptions(): Promise<Record<string, boolean>>;
/**
* Get connection state
*/
export declare function getConnectionState(): Promise<string>;
/**
* Add new subscription to channel
*/
export declare function addSubscription(channel: string): Promise<void>;
/**
* Remove subscription from channel
*/
export declare function removeSubscription(channel: string): Promise<void>;
/**
* Listen to Centrifugo events
*/
export declare function onCentrifugoEvent(event: 'connecting' | 'connected' | 'disconnected' | 'error' | 'subscribed' | 'unsubscribed' | 'subscribing' | 'publication', callback: (data: any) => void): Promise<UnlistenFn>;
export declare const onConnecting: (callback: (data: TauriEventWrapper<ConnectionStatus>) => void) => Promise<UnlistenFn>;
export declare const onConnected: (callback: (data: TauriEventWrapper<ConnectionStatus>) => void) => Promise<UnlistenFn>;
export declare const onDisconnected: (callback: (data: TauriEventWrapper<ConnectionStatus>) => void) => Promise<UnlistenFn>;
export declare const onError: (callback: (data: TauriEventWrapper<ErrorData>) => void) => Promise<UnlistenFn>;
export declare const onSubscribed: (callback: (data: TauriEventWrapper<ChannelStatus>) => void) => Promise<UnlistenFn>;
export declare const onUnsubscribed: (callback: (data: TauriEventWrapper<ChannelStatus>) => void) => Promise<UnlistenFn>;
export declare const onSubscribing: (callback: (data: TauriEventWrapper<ChannelStatus>) => void) => Promise<UnlistenFn>;
export declare const onPublication: (callback: (data: TauriEventWrapper<PublicationData>) => void) => Promise<UnlistenFn>;
export declare const onMessage: (callback: (data: {
channel: string;
data: any;
}) => void) => Promise<UnlistenFn>;
export declare const onChannelMessage: (channel: string, callback: (data: any) => void) => Promise<UnlistenFn>;
export declare const onAnyMessage: (callback: (data: {
channel: string;
data: any;
timestamp: number;
}) => void) => Promise<UnlistenFn>;
export declare const utils: {
/**
* Encode string to base64
*/
encode: (data: string) => string;
/**
* Decode base64 to string
*/
decode: (data: string) => string;
/**
* Encode object to base64 JSON
*/
encodeJson: (data: any) => string;
/**
* Decode base64 JSON to object
*/
decodeJson: <T>(data: string) => T;
};
export declare const helpers: {
/**
* Publish JSON data to channel (automatically encodes to base64)
*/
publishJson: (channel: string, data: any) => Promise<void>;
/**
* Execute RPC with JSON data (automatically encodes/decodes)
*/
rpcJson: (method: string, data: any) => Promise<any>;
/**
* Subscribe to channel and listen for messages
*/
subscribeToChannel: (channel: string, callback: (data: any) => void) => Promise<UnlistenFn>;
/**
* Subscribe to multiple channels
*/
subscribeToChannels: (channels: string[], callback: (data: {
channel: string;
data: any;
}) => void) => Promise<UnlistenFn[]>;
/**
* Wait for connection to be established
*/
waitForConnection: (timeout?: number) => Promise<boolean>;
/**
* Get connection status with retry
*/
getConnectionStatus: (retries?: number) => Promise<string>;
};