import * as plugins from './typedsocket.plugins.js'; export type TTypedSocketSide = 'server' | 'client'; export type TConnectionStatus = 'new' | 'connecting' | 'connected' | 'disconnected' | 'reconnecting'; /** * Options for creating a TypedSocket client */ export interface ITypedSocketClientOptions { autoReconnect?: boolean; maxRetries?: number; initialBackoffMs?: number; maxBackoffMs?: number; abortSignal?: AbortSignal; } /** * Lifecycle controls for one TypedRequest sent over a TypedSocket. */ export interface ITypedSocketRequestOptions { timeoutMs?: number; abortSignal?: AbortSignal; } /** * Wrapper for SmartServe's IWebSocketPeer to provide tag compatibility */ export interface ISmartServeConnectionWrapper { peer: plugins.IWebSocketPeer; getTagById(tagId: string): Promise<{ id: string; payload: any; } | undefined>; } export declare class TypedSocket { /** * Creates a TypedSocket client using native WebSocket. * Works in both browser and Node.js environments. * * @param typedrouterArg - TypedRouter for handling server-initiated requests * @param serverUrlArg - Server URL (e.g., 'http://localhost:3000' or 'wss://example.com') * @param options - Connection options * * @example * ```typescript * const typedRouter = new TypedRouter(); * const client = await TypedSocket.createClient( * typedRouter, * 'http://localhost:3000', * { autoReconnect: true } * ); * ``` */ static createClient(typedrouterArg: plugins.typedrequest.TypedRouter, serverUrlArg: string, options?: ITypedSocketClientOptions): Promise; /** * Returns the current window location origin URL. * Useful in browser environments for connecting to the same origin. */ static useWindowLocationOriginUrl: () => string; /** * Creates a TypedSocket server from an existing SmartServe instance. * This is the only way to create a server-side TypedSocket. * * @param smartServeArg - SmartServe instance with typedRouter configured in websocket options * @param typedRouterArg - TypedRouter for handling requests (must match SmartServe's typedRouter) * * @example * ```typescript * const typedRouter = new TypedRouter(); * const smartServe = new SmartServe({ * port: 3000, * websocket: { * typedRouter, * onConnectionOpen: (peer) => peer.tags.add('client') * } * }); * await smartServe.start(); * const typedSocket = TypedSocket.fromSmartServe(smartServe, typedRouter); * ``` */ static fromSmartServe(smartServeArg: plugins.SmartServe, typedRouterArg: plugins.typedrequest.TypedRouter): TypedSocket; /** * Registers built-in TypedHandlers for tag management */ private static registerTagHandlers; readonly side: TTypedSocketSide; readonly typedrouter: plugins.typedrequest.TypedRouter; statusSubject: plugins.smartrx.rxjs.Subject; private connectionStatus; private websocket; private clientOptions; private serverUrl; private retryCount; private currentBackoff; private stopped; private abortSignalListener?; private stopListeners; private pendingRequests; private clientTags; private clientTagMutations; private smartServeRef; private serverStopping; private unsubscribeSmartServeConnectionClose; private pendingServerRequests; private pendingServerRequestIdsByPeerId; private constructor(); /** * Connects the client to the server using native WebSocket */ private connect; /** * Converts an HTTP(S) URL to a WebSocket URL */ private toWebSocketUrl; /** * Handles incoming WebSocket messages */ private handleMessage; /** * Handles WebSocket disconnection */ private handleDisconnect; /** * Schedules a reconnection attempt with exponential backoff */ private scheduleReconnect; /** * Lifecycle chatter: hidden by default in browser devtools (verbose level) * and irrelevant for servers, but available when debugging. */ private logDebug; /** * Failures that reconnection is expected to recover from. Logged as debug * while retries remain; the final failure is reported by scheduleReconnect. */ private logTransient; private shouldReconnect; private waitForReconnectDelay; private createAbortError; private rejectPendingClientRequests; /** * Updates connection status and notifies subscribers */ private updateStatus; /** * Sends a request to the server and waits for response (client-side) */ private sendRequest; private normalizeRequestTimeout; private cancelPendingServerRequestsForPeer; private sendServerRequest; /** * Creates a TypedRequest for the specified method. * On clients, sends to the server. * On servers, sends to the specified target connection. */ createTypedRequest(methodName: T['method'], targetConnection?: ISmartServeConnectionWrapper, optionsArg?: ITypedSocketRequestOptions): plugins.typedrequest.TypedRequest; /** * Gets the current connection status */ getStatus(): TConnectionStatus; /** * Stops the TypedSocket client or cleans up server state */ stop(): Promise; /** * Sets a tag on this client connection. * Tags are stored on the server and can be used for filtering. * @client-only */ setTag(name: T['name'], payload: T['payload']): Promise; /** * Re-applies all client tags after a reconnect, since server-side tags are * bound to the previous connection. */ private reapplyClientTags; /** * Removes a tag from this client connection. * @client-only */ removeTag(name: string): Promise; /** * Finds all connections matching the filter function. * @server-only */ findAllTargetConnections(asyncFindFuncArg: (connectionArg: ISmartServeConnectionWrapper) => Promise): Promise; /** * Finds the first connection matching the filter function. * @server-only */ findTargetConnection(asyncFindFuncArg: (connectionArg: ISmartServeConnectionWrapper) => Promise): Promise; /** * Finds all connections with the specified tag. * @server-only */ findAllTargetConnectionsByTag(keyArg: TTag['name'], payloadArg?: TTag['payload']): Promise; /** * Finds the first connection with the specified tag. * @server-only */ findTargetConnectionByTag(keyArg: TTag['name'], payloadArg?: TTag['payload']): Promise; }