import { BaseChannel } from './base-channel.js'; import { MessageId } from './types.js'; /** * @category Channels * WebSocket channel implementation optimized for Node.js environments. * * Provides reliable WebSocket communication for server-side applications, * with automatic reconnection, robust error handling, and full type-safety. * Uses the popular 'ws' library for enhanced Node.js WebSocket support. * * ## Key Differences from BrowserChannel * * - **Server Optimized**: Better performance and memory usage for Node.js * - **Enhanced Error Handling**: More detailed error information and callbacks * - **Callback-based Sending**: Uses Node.js callback pattern for send operations * - **Buffer Support**: Can handle Node.js Buffer objects efficiently * - **Process Integration**: Works well with Node.js process lifecycle * * ## Use Cases * * - **Microservice Communication**: Connect services via WebSocket * - **Bot Implementations**: Build chat bots, game bots, etc. * - **Server-to-Server**: Real-time communication between servers * - **CLI Applications**: Interactive command-line tools with live updates * - **Backend Workers**: Background processes that need real-time updates * - **API Gateways**: Proxy and transform WebSocket connections * * ## Reconnection Behavior * * Identical exponential backoff strategy as BrowserChannel but optimized * for server environments with better error reporting and resource cleanup. * * @example * Basic server usage: * ```typescript * import { NodeChannel, defineMessage, z } from '@xtr-dev/zodiac'; * * const channel = new NodeChannel(); * await channel.connect('ws://management-server:8080'); * ``` * * @example * Microservice communication: * ```typescript * const channel = new NodeChannel(); * channel.setReconnectOptions(50, 1000); // Very resilient * await channel.connect('ws://user-service:3001/events'); * ``` */ export declare class NodeChannel extends BaseChannel { private ws; private reconnectAttempts; private maxReconnectAttempts; private reconnectDelay; /** * Establishes a WebSocket connection using the Node.js 'ws' library. * * Optimized for server environments with enhanced error reporting * and automatic reconnection handling. * * @param url - WebSocket server URL (ws:// or wss://) * @throws {Error} If already connected or connection is in progress * @throws {Error} If WebSocket creation fails * * @example * ```typescript * // Connect to local development server * await channel.connect('ws://localhost:8080'); * * // Connect to internal microservice * await channel.connect('ws://user-service:3001/websocket'); * * // Connect with authentication in URL * await channel.connect('wss://api.example.com/ws?token=abc123'); * ``` */ connect(url: string): Promise; disconnect(): Promise; send(id: MessageId, data: T): Promise; /** * Configures automatic reconnection behavior for Node.js environments. * * Particularly useful for microservices and server applications that * need high reliability and resilience to network issues. * * @param maxAttempts - Maximum number of reconnection attempts (default: 5) * @param baseDelay - Initial delay in milliseconds (default: 1000) * * @example * ```typescript * // High-reliability microservice configuration * channel.setReconnectOptions(100, 500); * * // Development environment (quick failure) * channel.setReconnectOptions(3, 1000); * * // Critical service (very persistent) * channel.setReconnectOptions(1000, 2000); * ``` */ setReconnectOptions(maxAttempts: number, baseDelay: number): void; } //# sourceMappingURL=node-channel.d.ts.map