import type { Readable } from "svelte/store"; import type { Fn } from "svelteshareds"; export declare type WebSocketStatus = "OPEN" | "CONNECTING" | "CLOSED"; export interface WebSocketOptions { on_connected?: (ws: WebSocket) => void; on_disconnected?: (ws: WebSocket, event: CloseEvent) => void; on_error?: (ws: WebSocket, event: Event) => void; on_message?: (ws: WebSocket, event: MessageEvent) => void; /** * Send heartbeat for every x milliseconds passed * * @default false */ heartbeat?: boolean | { /** * Message for the heartbeat * * @default 'ping' */ message?: string | ArrayBuffer | Blob; /** * Interval, in milliseconds * * @default 1000 */ interval?: number; /** * Heartbeat response timeout, in milliseconds * * @default 1000 */ pong_timeout?: number; }; /** * Enabled auto reconnect * * @default false */ auto_reconnect?: boolean | { /** * Maximum retry times. * * Or you can pass a predicate function (which returns true if you want to retry). * * @default -1 */ retries?: number | (() => boolean); /** * Delay for reconnect, in milliseconds * * @default 1000 */ delay?: number; /** * On maximum retry times reached. */ on_failed?: Fn; }; /** * Automatically open a connection * * @default true */ immediate?: boolean; /** * Automatically close a connection * * @default true */ auto_close?: boolean; /** * List of one or more sub-protocol strings * * @default [] */ protocols?: string[]; } export interface WebSocketReturn { /** * Reference to the latest data received via the websocket, * can be watched to respond to incoming messages */ data: Readable; /** * The current websocket status, can be only one of: * 'OPEN', 'CONNECTING', 'CLOSED' */ status: Readable; /** * Closes the websocket connection gracefully. */ close: WebSocket["close"]; /** * Reopen the websocket connection. * If there the current one is active, will close it before opening a new one. */ open: Fn; /** * Sends data through the websocket connection. * * @param data * @param buffer when the socket is not yet open, store the data into the buffer and sent them one connected. Default to true. */ send: (data: string | ArrayBuffer | Blob, buffer?: boolean) => boolean; /** * Reference to the WebSocket instance. */ ws: Readable; } /** * Reactive WebSocket client. * * @param url */ export declare function websocket(url: string, options?: WebSocketOptions): WebSocketReturn;