import type { RPCClient } from './client.js'; export interface ChannelMessage { type: 'message' | 'close' | 'error'; data?: any; error?: string; sender?: string; } /** * Bidirectional communication channel between RPC clients */ export declare class Channel { protected client: RPCClient; protected channelId: string; protected closed: boolean; protected unsubscribe?: () => void; protected initialized: boolean; /** Cached NATS subjects — built once instead of per send/request. */ private readonly subject; private readonly requestSubject; private handlers; private closeHandlers; private errorHandlers; protected subscriptions: Promise<() => void>[]; protected readonly sender: string; /** * Check if channel is closed */ get isClosed(): boolean; /** * Get the channel ID */ get id(): string; constructor(client: RPCClient, channelId: string); /** * Initialize the channel (called by RPCClient) */ init(): Promise; /** * Send data through the channel */ send(data: TMessage): Promise; /** * Send a request and wait for reply */ request(data: TRequest, timeout?: number): Promise; /** * Setup a request handler for this channel */ onRequest(handler: (data: TRequest) => Promise | TResponse): Promise<() => void>; /** * Listen for messages */ on(event: 'message', handler: (data: TEvent) => void): void; on(event: 'close', handler: () => void): void; on(event: 'error', handler: (error: Error) => void): void; /** * Remove event listener */ off(event: 'message', handler: (data: TEvent) => void): void; off(event: 'close', handler: () => void): void; off(event: 'error', handler: (error: Error) => void): void; /** * Close the channel gracefully */ close(): Promise; private handlerChain; private runHandler; protected emit(event: string, data?: any): void; protected handleClose(): void; protected handleError(error: Error): void; protected cleanup(): Promise; } /** * Private channel for 1:1 communication between two specific clients */ export declare class PrivateChannel extends Channel { private targetClientId; private readonly clientId; private remoteClientId?; /** Cached private subjects — the sorted-id join is built once here. */ private readonly privateSubject; private readonly privateRequestSubject; /** * Get the remote client ID (if connected) */ get remoteId(): string | undefined; constructor(client: RPCClient, channelId: string, targetClientId: string); /** * Initialize private channel with handshake */ init(): Promise; /** * Send data through the private channel */ send(data: TMessage): Promise; /** * Send a request and wait for reply using native NATS request/reply */ request(data: TRequest, timeout?: number): Promise; /** * Setup a request handler for this private channel */ onRequest(handler: (data: TRequest) => Promise | TResponse): Promise<() => void>; /** * Close the private channel gracefully */ close(): Promise; /** * Check if channel is connected to a specific client */ isConnectedTo(clientId: string): boolean; private sendRaw; }