@polymarket/real-time-data-client
Version:
A TypeScript client to receive real time data messages
111 lines (110 loc) • 4.01 kB
TypeScript
import { SubscriptionMessage, Message, ConnectionStatus } from "./model";
/**
* Interface representing the arguments for initializing a RealTimeDataClient.
*/
export interface RealTimeDataClientArgs {
/**
* Optional callback function that is called when the client successfully connects.
* @param client - The instance of the RealTimeDataClient that has connected.
*/
onConnect?: (client: RealTimeDataClient) => void;
/**
* Optional callback function that is called when the client receives a message.
* @param client - The instance of the RealTimeDataClient that received the message.
* @param message - The message received by the client.
*/
onMessage?: (client: RealTimeDataClient, message: Message) => void;
/**
* Optional callback function that is called when the client receives a connection status update.
* @param status - The connection status of the client.
*/
onStatusChange?: (status: ConnectionStatus) => void;
/**
* Optional host address to connect to.
*/
host?: string;
/**
* Optional interval in milliseconds for sending ping messages to keep the connection alive.
*/
pingInterval?: number;
/**
* Optional flag to enable or disable automatic reconnection when the connection is lost.
*/
autoReconnect?: boolean;
}
/**
* A client for managing real-time WebSocket connections, handling messages, subscriptions,
* and automatic reconnections.
*/
export declare class RealTimeDataClient {
/** WebSocket server host URL */
private readonly host;
/** Interval (in milliseconds) for sending ping messages */
private readonly pingInterval;
/** Determines whether the client should automatically reconnect on disconnection */
private autoReconnect;
/** Callback function executed when the connection is established */
private readonly onConnect?;
/** Callback function executed when a custom message is received */
private readonly onCustomMessage?;
/** Callback function executed on a connection status update */
private readonly onStatusChange?;
/** WebSocket instance */
private ws;
/**
* Constructs a new RealTimeDataClient instance.
* @param args Configuration options for the client.
*/
constructor(args?: RealTimeDataClientArgs);
/**
* Establishes a WebSocket connection to the server.
*/
connect(): this;
/**
* Handles WebSocket 'open' event. Executes the `onConnect` callback and starts pinging.
*/
private onOpen;
/**
* Handles WebSocket 'pong' event. Continues the ping cycle.
*/
private onPong;
/**
* Handles WebSocket errors. Logs the error and attempts reconnection if `autoReconnect` is enabled.
* @param err Error object describing the issue.
*/
private onError;
/**
* Handles WebSocket 'close' event. Logs the disconnect reason and attempts reconnection if `autoReconnect` is enabled.
* @param code Close event code.
* @param reason Buffer containing the reason for closure.
*/
private onClose;
/**
* Sends a ping message to keep the connection alive.
*/
private ping;
/**
* Handles incoming WebSocket messages. Parses and processes custom messages if applicable.
* @param event Raw WebSocket message data.
*/
private onMessage;
/**
* Closes the WebSocket connection.
*/
disconnect(): void;
/**
* Subscribes to a data stream by sending a subscription message.
* @param msg Subscription request message.
*/
subscribe(msg: SubscriptionMessage): void;
/**
* Unsubscribes from a data stream by sending an unsubscription message.
* @param msg Unsubscription request message.
*/
unsubscribe(msg: SubscriptionMessage): void;
/**
* Callback for connection status changes
* @param status status of the connection
*/
private notifyStatusChange;
}