Skip to content

API Reference


API Reference / WebSocketManager

Class: WebSocketManager

Defined in: src/ws/WebSocketManager.ts:80

Enhanced WebSocket Manager with auto-reconnect, batching, and Greeks.

This wrapper around DhanWS provides:

  • Automatic instrument batching (max 5000 per subscription)
  • Exponential backoff reconnection
  • State restoration (re-subscription after disconnect)
  • Real-time Greeks calculation on ticks
  • Underlying spot price tracking

Example

ts
const wsManager = new WebSocketManager({
  token: accessToken,
  clientId: client_id,
  maxInstrumentsPerBatch: 4000,
  enableAutoReconnect: true,
  enableGreeksCalculation: true,
  defaultRiskFreeRate: 0.065,
});

// Subscribe to entire BankNifty weekly chain
const bankNiftyStrikes = [44000, 44050, 44100, ...]; // 50 strikes
const subscriptions = bankNiftyStrikes.flatMap(strike => [
  { exchangeSegment: "NSE_FNO", securityId: getCallId(strike) },
  { exchangeSegment: "NSE_FNO", securityId: getPutId(strike) },
]);

wsManager.subscribe(subscriptions, {
  "44000": { strike: 44000, expiryDate: "2026-01-30", optionType: "call", ... },
  // ... more contracts
});

// Listen to enriched ticks with Greeks
wsManager.onTick((tick) => {
  if (tick.greeks) {
    console.log(`Delta: ${tick.greeks.delta}, Theta: ${tick.greeks.theta}`);
  }
});

await wsManager.connect();

Constructors

Constructor

new WebSocketManager(config): WebSocketManager

Defined in: src/ws/WebSocketManager.ts:93

Parameters

config

WebSocketManagerConfig

Returns

WebSocketManager

Methods

connect()

connect(): Promise<void>

Defined in: src/ws/WebSocketManager.ts:126

Connect to WebSocket servers.

Returns

Promise<void>


disconnect()

disconnect(): void

Defined in: src/ws/WebSocketManager.ts:133

Disconnect from WebSocket servers.

Returns

void


subscribe()

subscribe(instruments, optionMetadataMap?): void

Defined in: src/ws/WebSocketManager.ts:146

Subscribe to instruments with automatic batching.

If the number of instruments exceeds the batch size, they are split into multiple subscription requests automatically.

Parameters

instruments

InstrumentSubscription[]

Array of instrument subscriptions

optionMetadataMap?

Record<string, OptionContractMetadata>

Optional metadata for options (for Greeks calculation)

Returns

void


unsubscribe()

unsubscribe(instruments): void

Defined in: src/ws/WebSocketManager.ts:177

Unsubscribe from instruments.

Parameters

instruments

InstrumentSubscription[]

Returns

void


updateUnderlyingSpot()

updateUnderlyingSpot(underlyingSecurityId, spotPrice): void

Defined in: src/ws/WebSocketManager.ts:195

Update underlying spot price (used for Greeks calculation).

Parameters

underlyingSecurityId

string

Security ID of the underlying index (e.g., "1333" for Nifty)

spotPrice

number

Current spot price

Returns

void


onTick()

onTick(callback): void

Defined in: src/ws/WebSocketManager.ts:202

Set callback for enriched ticks (with Greeks).

Parameters

callback

(tick) => void

Returns

void


getSubscribedInstruments()

getSubscribedInstruments(): InstrumentSubscription[]

Defined in: src/ws/WebSocketManager.ts:209

Get all currently subscribed instruments.

Returns

InstrumentSubscription[]


isConnected()

isConnected(): boolean

Defined in: src/ws/WebSocketManager.ts:216

Check if connected.

Returns

boolean


getLTPStore()

getLTPStore(): LTPStore

Defined in: src/ws/WebSocketManager.ts:223

Get LTP store reference.

Returns

LTPStore

Community project — not affiliated with Dhan. MIT License.