smartapi-typescript
Version:
TypeScript library for Angel One SmartAPI broker API
146 lines (145 loc) • 4.88 kB
TypeScript
import { EventEmitter } from 'events';
import { ApiResponse } from '../types';
/**
* WebSocket feed modes according to WebSocket Streaming 2.0 specification
*/
export declare enum FeedMode {
LTP = 1,// Last traded price only
QUOTE = 2,// Quote data (OHLC, LTP, etc.)
SNAP_QUOTE = 3,// Snap quote (includes best 5 prices)
DEPTH_20 = 4
}
/**
* Exchange types for WebSocket API
*/
export declare enum ExchangeType {
NSE_CM = 1,// NSE Cash Market
NSE_FO = 2,// NSE Futures & Options
BSE_CM = 3,// BSE Cash Market
BSE_FO = 4,// BSE Futures & Options
MCX_FO = 5,// MCX Futures & Options
NCX_FO = 7,// NCX Futures & Options
CDE_FO = 13
}
/**
* SmartAPI WebSocket client for real-time market data
* Implementation based on WebSocket Streaming 2.0 specification
*/
export declare class SmartAPIWebSocket extends EventEmitter {
private apiKey;
private clientCode;
private feedToken;
private jwtToken;
private wsUrl;
private ws;
private connectionState;
private subscriptions;
private reconnectAttempts;
private maxReconnectAttempts;
private reconnectInterval;
private heartbeatInterval;
private debug;
private pendingSubscriptions;
private isBrowser;
/**
* Initialize a new SmartAPIWebSocket client
* @param apiKey API key provided by Angel One
* @param clientCode Client code/user id for Angel One
* @param feedToken Feed token for WebSocket authentication
* @param jwtToken JWT token from login API (auth token)
* @param debug Enable debug logging
*/
constructor(apiKey: string, clientCode: string, feedToken: string, jwtToken: string, debug?: boolean);
/**
* Log debug messages if debug mode is enabled
* @param message Message to log
* @param data Optional data to log
*/
private log;
/**
* Connect to the WebSocket server
*/
connect(): Promise<boolean>;
/**
* Attempt to reconnect to the WebSocket server
*/
private attemptReconnect;
/**
* Set up heartbeat to keep the connection alive
*/
private setupHeartbeat;
/**
* Clear the heartbeat interval
*/
private clearHeartbeat;
/**
* Parse binary message received from WebSocket
* @param data Binary data buffer
*/
private parseBinaryMessage;
/**
* Process any pending subscriptions after successful connection
*/
private processPendingSubscriptions;
/**
* Send subscription/unsubscription request
* @param correlationId Correlation ID for the request
* @param feedMode Feed mode
* @param tokenList List of exchange and token pairs
* @param action Action (SUBSCRIBE or UNSUBSCRIBE)
*/
private sendSubscriptionRequest;
/**
* Generate a subscription key from exchange type and token
* @param exchangeType Exchange type
* @param token Symbol token
* @returns Subscription key
*/
private getSubscriptionKey;
/**
* Subscribe to market data for a symbol
* @param correlationId Client-defined correlation ID for this subscription
* @param exchangeType Exchange type
* @param token Symbol token
* @param feedMode Feed mode (LTP, QUOTE, SNAP_QUOTE, DEPTH_20)
* @returns Subscription result
*/
subscribe(correlationId: string, exchangeType: ExchangeType, token: string, feedMode?: FeedMode): Promise<ApiResponse>;
/**
* Subscribe to multiple symbols at once
* @param correlationId Client-defined correlation ID for this subscription batch
* @param symbols Array of {exchangeType, token} objects
* @param feedMode Feed mode (LTP, QUOTE, SNAP_QUOTE, DEPTH_20)
* @returns Subscription result
*/
subscribeMultiple(correlationId: string, symbols: Array<{
exchangeType: ExchangeType;
token: string;
}>, feedMode?: FeedMode): Promise<ApiResponse>;
/**
* Unsubscribe from market data for a symbol
* @param correlationId Client-defined correlation ID for this unsubscription
* @param exchangeType Exchange type
* @param token Symbol token
* @returns Unsubscription result
*/
unsubscribe(correlationId: string, exchangeType: ExchangeType, token: string): Promise<ApiResponse>;
/**
* Unsubscribe from multiple symbols at once
* @param correlationId Client-defined correlation ID for this unsubscription batch
* @param symbols Array of {exchangeType, token} objects
* @returns Unsubscription result
*/
unsubscribeMultiple(correlationId: string, symbols: Array<{
exchangeType: ExchangeType;
token: string;
}>): Promise<ApiResponse>;
/**
* Resubscribe to all active subscriptions
*/
private resubscribe;
/**
* Disconnect from the WebSocket server
*/
disconnect(): void;
}