@push.rocks/smartipc
Version:
A library for node inter process communication, providing an easy-to-use API for IPC.
102 lines (101 loc) • 2.96 kB
TypeScript
import * as plugins from './smartipc.plugins.js';
import type { IIpcChannelOptions } from './classes.ipcchannel.js';
/**
* Options for IPC Client
*/
export interface IConnectRetryConfig {
/** Enable connection retry */
enabled: boolean;
/** Initial delay before first retry in ms */
initialDelay?: number;
/** Maximum delay between retries in ms */
maxDelay?: number;
/** Maximum number of attempts */
maxAttempts?: number;
/** Total timeout for all retry attempts in ms */
totalTimeout?: number;
}
export interface IClientConnectOptions {
/** Wait for server to be ready before attempting connection */
waitForReady?: boolean;
/** Timeout for waiting for server readiness in ms */
waitTimeout?: number;
}
export interface IIpcClientOptions extends IIpcChannelOptions {
/** Client identifier */
clientId?: string;
/** Client metadata */
metadata?: Record<string, any>;
/** Connection retry configuration */
connectRetry?: IConnectRetryConfig;
/** Registration timeout in ms (default: 5000) */
registerTimeoutMs?: number;
}
/**
* IPC Client for connecting to an IPC server
*/
export declare class IpcClient extends plugins.EventEmitter {
private options;
private channel;
private messageHandlers;
private isConnected;
private clientId;
private didRegisterOnce;
constructor(options: IIpcClientOptions);
/**
* Connect to the server
*/
connect(connectOptions?: IClientConnectOptions): Promise<void>;
/**
* Attempt to register this client over the current channel connection.
* Sets connection flags and emits 'connect' on success.
*/
private attemptRegistrationInternal;
/**
* Disconnect from the server
*/
disconnect(): Promise<void>;
/**
* Setup channel event handlers
*/
private setupChannelHandlers;
/**
* Register a message handler
*/
onMessage(type: string, handler: (payload: any) => any | Promise<any>): void;
/**
* Send a message to the server
*/
sendMessage(type: string, payload: any, headers?: Record<string, any>): Promise<void>;
/**
* Send a request to the server and wait for response
*/
request<TReq = any, TRes = any>(type: string, payload: TReq, options?: {
timeout?: number;
headers?: Record<string, any>;
}): Promise<TRes>;
/**
* Subscribe to a topic (pub/sub pattern)
*/
subscribe(topic: string, handler: (payload: any) => void): Promise<void>;
/**
* Unsubscribe from a topic
*/
unsubscribe(topic: string): Promise<void>;
/**
* Publish to a topic
*/
publish(topic: string, payload: any): Promise<void>;
/**
* Get client ID
*/
getClientId(): string;
/**
* Check if client is connected
*/
getIsConnected(): boolean;
/**
* Get client statistics
*/
getStats(): any;
}