KiteTicker
in package
KiteTicker - WebSocket client for streaming live market data
Table of Contents
- EXCHANGE_MAP = ["nse" => 1, "nfo" => 2, "cds" => 3, "bse" => 4, "bfo" => 5, "bcd" => 6, "mcx" => 7, "mcxsx" => 8, "indices" => 9]
- MODE_FULL = "full"
- MODE_LTP = "ltp"
- MODE_QUOTE = "quote"
- STATE_CONNECTED = 2
- STATE_CONNECTING = 1
- STATE_DISCONNECTED = 0
- STATE_RECONNECTING = 3
- $accessToken : string
- $apiKey : string
- $autoReconnect : bool
- $callbacks : array<string|int, mixed>
- $connection : WebSocket|null
- $connectionState : int
- $debug : bool
- $maxReconnectAttempts : int
- $reconnectAttempts : int
- $reconnectDelay : int
- $subscribedTokens : array<string|int, mixed>
- $timeout : int
- $tokenModes : array<string|int, mixed>
- $wsUrl : string
- __construct() : mixed
- Initialize KiteTicker instance
- connect() : void
- Connect to WebSocket server
- disconnect() : void
- Disconnect from WebSocket server
- getConnectionState() : int
- Get current connection state
- getSubscribedTokens() : array<string|int, mixed>
- Get subscribed tokens
- isConnected() : bool
- Check if WebSocket connection is currently connected
- on() : void
- Bind callback to an event
- setAutoReconnect() : void
- Enable or disable auto-reconnect
- setMode() : void
- Set mode for subscribed instruments
- subscribe() : void
- Subscribe to instruments
- unsubscribe() : void
- Unsubscribe from instruments
- attemptReconnect() : void
- Attempt to reconnect to WebSocket
- convertTimestamp() : string
- Convert Unix timestamp to local time
- debugLog() : void
- Log debug messages
- handleConnection() : void
- Handle successful WebSocket connection
- handleConnectionError() : void
- Handle connection errors
- handleDisconnection() : void
- Handle WebSocket disconnection
- handleError() : void
- Handle WebSocket errors
- handleMessage() : void
- Handle incoming WebSocket messages
- isBinaryData() : bool
- Check if payload is binary market data
- parseBinary() : array<string|int, mixed>
- Parse binary market data
- parseFullModeData() : array<string|int, mixed>
- Parse full mode additional data
- parseIndicesData() : array<string|int, mixed>
- Parse indices data
- parsePacket() : array<string|int, mixed>|null
- Parse individual tick packet
- parseTextMessage() : void
- Parse text message (order updates, errors, etc.)
- parseTickData() : array<string|int, mixed>
- Parse tick data
- performConnection() : void
- Perform the actual WebSocket connection
- resubscribeAll() : void
- Resubscribe to all previously subscribed tokens
- sendModeChange() : void
- Send mode change message
- sendSubscription() : void
- Send subscription message
- sendUnsubscription() : void
- Send unsubscription message
- splitPackets() : array<string|int, mixed>
- Split binary message into individual packets
- trigger() : void
- Trigger event callbacks
- unpackBinary() : int
- Unpack binary data
Constants
EXCHANGE_MAP
public
mixed
EXCHANGE_MAP
= ["nse" => 1, "nfo" => 2, "cds" => 3, "bse" => 4, "bfo" => 5, "bcd" => 6, "mcx" => 7, "mcxsx" => 8, "indices" => 9]
MODE_FULL
public
mixed
MODE_FULL
= "full"
MODE_LTP
public
mixed
MODE_LTP
= "ltp"
MODE_QUOTE
public
mixed
MODE_QUOTE
= "quote"
STATE_CONNECTED
public
mixed
STATE_CONNECTED
= 2
STATE_CONNECTING
public
mixed
STATE_CONNECTING
= 1
STATE_DISCONNECTED
public
mixed
STATE_DISCONNECTED
= 0
STATE_RECONNECTING
public
mixed
STATE_RECONNECTING
= 3
Properties
$accessToken
private
string
$accessToken
$apiKey
private
string
$apiKey
$autoReconnect
private
bool
$autoReconnect
= true
$callbacks
private
array<string|int, mixed>
$callbacks
= ['connect' => [], 'disconnect' => [], 'ticks' => [], 'order_update' => [], 'error' => [], 'reconnect' => [], 'close' => [], 'message' => [], 'noreconnect' => []]
$connection
private
WebSocket|null
$connection
= null
$connectionState
private
int
$connectionState
= self::STATE_DISCONNECTED
$debug
private
bool
$debug
= false
$maxReconnectAttempts
private
int
$maxReconnectAttempts
= 5
$reconnectAttempts
private
int
$reconnectAttempts
= 0
$reconnectDelay
private
int
$reconnectDelay
= 3
$subscribedTokens
private
array<string|int, mixed>
$subscribedTokens
= []
$timeout
private
int
$timeout
$tokenModes
private
array<string|int, mixed>
$tokenModes
= []
$wsUrl
private
string
$wsUrl
Methods
__construct()
Initialize KiteTicker instance
public
__construct(string $apiKey, string $accessToken[, int $timeout = 30 ][, bool $autoReconnect = true ][, bool $debug = false ][, string $root = "wss://ws.kite.trade/" ]) : mixed
Parameters
- $apiKey : string
-
Kite Connect API key
- $accessToken : string
-
Access token obtained after login
- $timeout : int = 30
-
Connection timeout in seconds
- $autoReconnect : bool = true
-
Enable automatic reconnection
- $debug : bool = false
-
Enable debug mode
- $root : string = "wss://ws.kite.trade/"
-
WebSocket root URL
Return values
mixed —connect()
Connect to WebSocket server
public
connect() : void
Tags
Return values
void —disconnect()
Disconnect from WebSocket server
public
disconnect() : void
Return values
void —getConnectionState()
Get current connection state
public
getConnectionState() : int
Return values
int —Connection state constant
getSubscribedTokens()
Get subscribed tokens
public
getSubscribedTokens() : array<string|int, mixed>
Return values
array<string|int, mixed> —List of subscribed instrument tokens
isConnected()
Check if WebSocket connection is currently connected
public
isConnected() : bool
Return values
bool —True if connected
on()
Bind callback to an event
public
on(string $event, callable $callback) : void
Parameters
- $event : string
-
Event name (connect, disconnect, ticks, error, etc.)
- $callback : callable
-
Callback function (Closure or string function name)
Return values
void —setAutoReconnect()
Enable or disable auto-reconnect
public
setAutoReconnect(bool $enabled[, int $maxAttempts = 5 ][, int $delay = 3 ]) : void
Parameters
- $enabled : bool
-
Whether to enable auto-reconnect
- $maxAttempts : int = 5
-
Maximum reconnection attempts
- $delay : int = 3
-
Delay between attempts in seconds
Return values
void —setMode()
Set mode for subscribed instruments
public
setMode(string $mode[, array<string|int, mixed> $tokens = [] ]) : void
Parameters
- $mode : string
-
New streaming mode
- $tokens : array<string|int, mixed> = []
-
List of instrument tokens (empty for all)
Tags
Return values
void —subscribe()
Subscribe to instruments
public
subscribe(array<string|int, mixed> $tokens[, string $mode = self::MODE_QUOTE ]) : void
Parameters
- $tokens : array<string|int, mixed>
-
List of instrument tokens
- $mode : string = self::MODE_QUOTE
-
Streaming mode (ltp, quote, full)
Tags
Return values
void —unsubscribe()
Unsubscribe from instruments
public
unsubscribe(array<string|int, mixed> $tokens) : void
Parameters
- $tokens : array<string|int, mixed>
-
List of instrument tokens to unsubscribe
Return values
void —attemptReconnect()
Attempt to reconnect to WebSocket
private
attemptReconnect() : void
Return values
void —convertTimestamp()
Convert Unix timestamp to local time
private
convertTimestamp(int $timestamp) : string
Parameters
- $timestamp : int
-
Unix timestamp
Return values
string —Formatted local time
debugLog()
Log debug messages
private
debugLog(string $message) : void
Parameters
- $message : string
-
Debug message
Return values
void —handleConnection()
Handle successful WebSocket connection
private
handleConnection(WebSocket $conn) : void
Parameters
- $conn : WebSocket
-
WebSocket connection instance
Return values
void —handleConnectionError()
Handle connection errors
private
handleConnectionError(Exception $e) : void
Parameters
- $e : Exception
-
Connection error
Return values
void —handleDisconnection()
Handle WebSocket disconnection
private
handleDisconnection([int|null $code = null ][, string|null $reason = null ]) : void
Parameters
- $code : int|null = null
-
Disconnect code
- $reason : string|null = null
-
Disconnect reason
Return values
void —handleError()
Handle WebSocket errors
private
handleError(Exception $e) : void
Parameters
- $e : Exception
-
Error exception
Return values
void —handleMessage()
Handle incoming WebSocket messages
private
handleMessage(MessageInterface $msg) : void
Parameters
- $msg : MessageInterface
-
Incoming message
Return values
void —isBinaryData()
Check if payload is binary market data
private
isBinaryData(string $payload) : bool
Parameters
- $payload : string
-
Message payload
Return values
bool —True if binary data
parseBinary()
Parse binary market data
private
parseBinary(string $msg) : array<string|int, mixed>
Parameters
- $msg : string
-
Binary message
Return values
array<string|int, mixed> —Parsed tick data
parseFullModeData()
Parse full mode additional data
private
parseFullModeData(array<string|int, mixed> $packet, float $divisor) : array<string|int, mixed>
Parameters
- $packet : array<string|int, mixed>
-
Packet data
- $divisor : float
-
Price divisor
Return values
array<string|int, mixed> —Additional full mode data
parseIndicesData()
Parse indices data
private
parseIndicesData(array<string|int, mixed> $packet, float $divisor) : array<string|int, mixed>
Parameters
- $packet : array<string|int, mixed>
-
Packet data
- $divisor : float
-
Price divisor
Return values
array<string|int, mixed> —Parsed data
parsePacket()
Parse individual tick packet
private
parsePacket(array<string|int, mixed> $packet) : array<string|int, mixed>|null
Parameters
- $packet : array<string|int, mixed>
-
Packet data
Return values
array<string|int, mixed>|null —Parsed tick data
parseTextMessage()
Parse text message (order updates, errors, etc.)
private
parseTextMessage(string $payload) : void
Parameters
- $payload : string
-
Text message payload
Return values
void —parseTickData()
Parse tick data
private
parseTickData(array<string|int, mixed> $packet, float $divisor) : array<string|int, mixed>
Parameters
- $packet : array<string|int, mixed>
-
Packet data
- $divisor : float
-
Price divisor
Return values
array<string|int, mixed> —Parsed data
performConnection()
Perform the actual WebSocket connection
private
performConnection() : void
Return values
void —resubscribeAll()
Resubscribe to all previously subscribed tokens
private
resubscribeAll() : void
Return values
void —sendModeChange()
Send mode change message
private
sendModeChange(string $mode, array<string|int, mixed> $tokens) : void
Parameters
- $mode : string
-
New streaming mode
- $tokens : array<string|int, mixed>
-
Instrument tokens
Return values
void —sendSubscription()
Send subscription message
private
sendSubscription(array<string|int, mixed> $tokens, string $mode) : void
Parameters
- $tokens : array<string|int, mixed>
-
Instrument tokens
- $mode : string
-
Streaming mode
Return values
void —sendUnsubscription()
Send unsubscription message
private
sendUnsubscription(array<string|int, mixed> $tokens) : void
Parameters
- $tokens : array<string|int, mixed>
-
Instrument tokens
Return values
void —splitPackets()
Split binary message into individual packets
private
splitPackets(string $msg) : array<string|int, mixed>
Parameters
- $msg : string
-
Binary message
Return values
array<string|int, mixed> —Array of packets
trigger()
Trigger event callbacks
private
trigger(string $event[, array<string|int, mixed> $args = [] ]) : void
Parameters
- $event : string
-
Event name
- $args : array<string|int, mixed> = []
-
Arguments to pass to callbacks
Return values
void —unpackBinary()
Unpack binary data
private
unpackBinary(string $msg, string $format, int $start, int $end) : int
Parameters
- $msg : string
-
Binary message
- $format : string
-
Unpack format
- $start : int
-
Start position
- $end : int
-
End position
Return values
int —Unpacked value