Skip to content

WebSocket Market Feed

Stream real-time LTP, OHLCV, and market depth over WebSocket — the low-latency alternative to REST polling.

Connection

ts
await client.ws.connect();

// Wait for the market feed socket to open
client.ws.market.once("open", () => {
  console.log("Market feed connected");
});

Subscribe to Instruments

Subscribe before connecting — subscriptions queue up and auto-send on open:

ts
client.ws.market.subscribe([
  { exchangeSegment: "NSE_EQ", securityId: "1333" },   // HDFCBANK
  { exchangeSegment: "NSE_FNO", securityId: "58072" }, // NIFTY FUT
  { exchangeSegment: "IDX_I", securityId: "13" },      // NIFTY 50
  { exchangeSegment: "MCX_COMM", securityId: "466583" }, // GOLD FUT
]);

await client.ws.connect();

Tick Events

ts
client.ws.market.on("tick", (tick) => {
  console.log(tick.exchangeSegment, tick.securityId, tick.ltp);
  // Full payload:
  //   ltp, volume, openInterest
  //   dayHigh, dayLow, dayOpen, dayClose
  //   5-level bid/ask (depth)
});

Market Depth

Enable 20-level (default) or 200-level depth:

ts
client.ws.enableDepth("twenty");    // default: 20 levels
client.ws.enableDepth("twohundred"); // 200 levels, one instrument per connection

client.ws.depth?.subscribe([
  { exchangeSegment: "NSE_EQ", securityId: "1333" },
]);

client.ws.depth?.on("depth", (event) => {
  console.log(event.levels); // top bid/ask levels
});

Unsubscribe

ts
client.ws.market.unsubscribe([
  { exchangeSegment: "NSE_EQ", securityId: "1333" },
]);

Subscription Modes

ModeDescription
tickerLTP only (lightest)
quoteLTP + OHLCV + depth
fullEverything (default)
ts
// Set mode in DhanWS options, or use enableDepth()

All Segments

The SDK supports all DhanHQ exchange segments:

SegmentDescription
NSE_EQNSE Equities
BSE_EQBSE Equities
NSE_FNONSE F&O (Futures & Options)
NSE_CURRENCYNSE Currency Derivatives
BSE_CURRENCYBSE Currency Derivatives
MCX_COMMMCX Commodities
IDX_IIndices (NIFTY, BANKNIFTY, etc.)
BSE_IDX_IBSE Indices

LTP Store

The SDK maintains an in-memory LTP store updated by the WebSocket feed:

ts
const ltp = client.ws.ltpStore.get("NSE_EQ:1333");
console.log("Latest LTP:", ltp);

For the binary protocol reference, see the WS Protocol docs.

Community project — not affiliated with Dhan. MIT License.