Order & Position Streaming API (WebSocket)

The Order & Position Streaming API provides real-time streaming updates for:

  • Order lifecycle events (new, validation, open, complete, rejected, etc.)
  • Live position updates

The API uses a secure WebSocket (wss://) connection and pushes updates instantly whenever state changes occur in the trading system.

This API eliminates the need for polling and ensures low-latency state synchronization.

{
    "wss://<baseurl>/realtime"
}

Replace with the value returned from the /tradeApiValidate API.

Example:

{
    "wss://e21.kotaksecurities.com/realtime"
}
const ws =newWebSocket(`wss://${baseurl}/realtime`);

Immediately after onopen, send authentication string:

{type:cn,Authorization:<token>,Sid:<sid>,src:WEB}

⚠️ Important:

  • This is NOT JSON.
  • Do NOT use JSON.stringify.
  • It must be sent as a single raw string.

On successful authentication, server responds:

{
"ak":"ok",
"type":"cn",
"task":"cn",
"msg":"connected"
}

The WebSocket sends two primary message types:

  1. order
  2. position

Each order update reflects a state change in the order lifecycle.

{
"type":"order",
"data":{
"nOrdNo":"260216000308219",
"ordSt":"complete",
"avgPrc":"35.88",
"qty":1,
"fldQty":1,
"unFldSz":0,
"sym":"ITBEES",
"trnsTp":"B",
"prcTp":"MKT",
"prod":"NRML",
"exSeg":"nse_cm",
"ordDtTm":"16-Feb-2026 12:29:31",
"exOrdId":"1100000049435826"
}
}

Example sequence:

  1. put order req received
  2. validation pending
  3. open pending
  4. open
  5. complete

Other possible states may include:

  • rejected
  • cancelled
  • modified

Position updates are pushed whenever there is:

  • New trade execution
  • Position quantity change
  • Buy/sell adjustment
{
"type":"position",
"data":{
"actId":"XP6M4",
"sym":"ITBEES",
"exSeg":"nse_cm",
"prod":"NRML",
"flBuyQty":"1",
"flSellQty":"0",
"buyAmt":"35.88",
"sellAmt":"0.00",
"posFlg":"true",
"hsUpTm":"2026/02/16 12:29:31"
}
}
ws.onmessage =(event) => {

if (message.type ==="order") {
console.log("Order Update:", message.data);
  }

if (message.type ==="position") {
console.log("Position Update:", message.data);
  }
};

  • WebSocket authentication must be sent as raw string (non-JSON).
  • Token and Sid must be valid.
  • Token expiry will terminate connection.
  • Reconnection logic should be implemented at client level.
  • All numeric financial fields are returned as strings unless otherwise specified.
  • Multiple order updates are sent for a single order as it moves through lifecycle stages.
  • Implement auto-reconnect with exponential backoff.
  • Detect duplicate order events using nOrdNo + updRecvTm.
  • Maintain in-memory order state machine.
  • Persist only terminal states (complete, rejected, cancelled).
  • Track latency: updRecvTm - boeSec.