Documentation Index
Fetch the complete documentation index at: https://mintlify.com/anurag-roy/kiteconnect-ts/llms.txt
Use this file to discover all available pages before exploring further.
KiteTicker emits named events throughout its lifecycle. You register handlers with ticker.on(event, callback) and the ticker calls them as events occur. You can register multiple callbacks for the same event — they are all invoked in registration order.
ticker.on("connect", callback);
ticker.on("ticks", callback);
ticker.on("disconnect", callback);
Event reference
connect
Fires when the WebSocket handshake completes and the connection is open. This is the right place to call subscribe() and setMode() — doing so here ensures your subscriptions are re-established automatically after every reconnection.
Signature: () => void
ticker.on("connect", () => {
console.log("Connected");
ticker.subscribe([738561]);
ticker.setMode(ticker.modeFull, [738561]);
});
ticks
Fires whenever the server sends a binary message containing one or more tick packets. The argument is an array of parsed Tick objects.
Signature: (ticks: Tick[]) => void
ticker.on("ticks", (ticks) => {
for (const tick of ticks) {
console.log(tick.instrument_token, tick.last_price);
}
});
Tick union type
A Tick is one of five shapes depending on the instrument type and the mode you set:
type Tick =
| TickLtp // mode: 'ltp' — all instruments
| TickIndexQuote // mode: 'quote' — index instruments
| TickIndexFull // mode: 'full' — index instruments
| TickQuote // mode: 'quote' — tradable instruments
| TickFull; // mode: 'full' — tradable instruments
Use the mode and tradable fields to narrow the type and access mode-specific fields safely:
ticker.on("ticks", (ticks: Tick[]) => {
for (const tick of ticks) {
if (tick.mode === "ltp") {
// tick is TickLtp — only last_price is available
console.log("LTP:", tick.last_price);
} else if (tick.mode === "quote" && tick.tradable) {
// tick is TickQuote — volume and OHLC are available
console.log("Volume:", tick.volume_traded, "OHLC:", tick.ohlc);
} else if (tick.mode === "full" && tick.tradable) {
// tick is TickFull — depth and OI are available
console.log("Depth buy[0]:", tick.depth.buy[0]);
console.log("OI:", tick.oi);
}
}
});
disconnect
Fires when the connection drops — either due to a network error, a server-side close, or a read timeout. The error argument describes the cause.
Signature: (error: Error) => void
ticker.on("disconnect", (error) => {
console.error("Disconnected:", error);
});
If auto-reconnect is enabled, the ticker automatically attempts to reconnect after this event. You do not need to call connect() manually.
error
Fires on a WebSocket-level error. The ticker force-closes the connection after this event to avoid ghost connections.
Signature: (error: Error) => void
ticker.on("error", (error) => {
console.error("WebSocket error:", error);
});
close
Fires when the connection closes cleanly (for example, after you call disconnect()).
Signature: () => void
ticker.on("close", () => {
console.log("Connection closed");
});
reconnect
Fires at the start of each reconnection attempt. Use this to log reconnection progress or update your UI.
Signature: (retries: number, interval: number) => void
| Argument | Type | Description |
|---|
retries | number | The current reconnection attempt count |
interval | number | Seconds until the next connect() call |
ticker.on("reconnect", (retries, interval) => {
console.log(`Reconnecting: attempt ${retries}, next try in ${interval}s`);
});
Exponential backoff algorithm
The reconnect interval grows as 2^attempt seconds, capped at max_delay:
| Attempt | Interval (max_delay = 60s) |
|---|
| 1 | 2s |
| 2 | 4s |
| 3 | 8s |
| 4 | 16s |
| 5 | 32s |
| 6+ | 60s (capped) |
noreconnect
Fires when the reconnection attempt count exceeds max_retry. At this point the ticker has given up trying to restore the connection.
Signature: () => void
After emitting noreconnect, the source calls process.exit(1). Register this event and perform any necessary cleanup — flushing logs, alerting on-call, etc. — before the process terminates.
ticker.on("noreconnect", () => {
console.error("Max reconnections reached — shutting down");
// flush logs, notify alerting systems, etc.
});
order_update
Fires when Kite sends a postback for an order state change on the connected user’s account. The argument is an Order object with timestamps parsed to Date instances.
Signature: (order: Order) => void
ticker.on("order_update", (order) => {
console.log("Order update:", order.order_id, order.status);
});
message
Fires for every raw binary ArrayBuffer received from the server, before any parsing. Use this for debugging or if you want to implement your own binary parser.
Signature: (data: ArrayBuffer) => void
ticker.on("message", (data) => {
console.log("Raw binary message, byte length:", data.byteLength);
});
Complete example
The example below wires up all events and demonstrates a production-ready setup with reconnection handling.
import { KiteTicker } from "kiteconnect-ts";
const ticker = new KiteTicker({
api_key: "api_key",
access_token: "access_token",
});
// Configure reconnection: up to 10 attempts, 5-second maximum interval
ticker.autoReconnect(true, 10, 5);
ticker.connect();
ticker.on("connect", () => {
console.log("Connected");
const tokens = [738561];
ticker.subscribe(tokens);
ticker.setMode(ticker.modeFull, tokens);
});
ticker.on("ticks", (ticks) => {
console.log("Ticks", ticks);
});
ticker.on("disconnect", (error) => {
console.warn("Disconnected:", error);
});
ticker.on("error", (error) => {
console.error("WebSocket error:", error);
});
ticker.on("close", () => {
console.log("Connection closed");
});
ticker.on("reconnect", (retries, interval) => {
console.log(`Reconnecting: attempt ${retries}, next in ${interval}s`);
});
ticker.on("noreconnect", () => {
console.error("Max reconnections reached — process will exit");
});
ticker.on("order_update", (order) => {
console.log("Order update received:", order);
});
ticker.on("message", (data) => {
console.log("Raw message received, bytes:", data.byteLength);
});