|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once 'vendor/autoload.php'; |
| 4 | + |
| 5 | +use KiteConnect\KiteConnect; |
| 6 | +use KiteConnect\KiteTicker; |
| 7 | + |
| 8 | +// Example usage of KiteTicker with the official KiteConnect client |
| 9 | + |
| 10 | +$apiKey = 'your_api_key_here'; |
| 11 | +$accessToken = 'your_access_token_here'; |
| 12 | + |
| 13 | +// Create ticker instance with proper configuration |
| 14 | +$ticker = new KiteTicker( |
| 15 | + $apiKey, |
| 16 | + $accessToken, |
| 17 | + 30, // timeout |
| 18 | + true, // auto-reconnect |
| 19 | + true // debug mode |
| 20 | +); |
| 21 | + |
| 22 | +// Connection event - fires when WebSocket connects successfully |
| 23 | +$ticker->on('connect', function() use ($ticker) { |
| 24 | + echo "Connected to KiteTicker WebSocket!\n"; |
| 25 | + |
| 26 | + // Subscribe to instruments after connection |
| 27 | + $ticker->subscribe([738561], KiteTicker::MODE_FULL); |
| 28 | + echo "Subscribed to instrument 738561 (RELIANCE) in FULL mode\n"; |
| 29 | +}); |
| 30 | + |
| 31 | +// Market data event - receives real-time ticks |
| 32 | +$ticker->on('ticks', function(array $ticks) { |
| 33 | + foreach ($ticks as $tick) { |
| 34 | + $token = $tick['instrument_token']; |
| 35 | + $price = $tick['last_price']; |
| 36 | + $volume = $tick['volume_traded']; |
| 37 | + $mode = $tick['mode']; |
| 38 | + |
| 39 | + echo "Token: {$token} | Price: {$price} | Volume: {$volume} | Mode: {$mode}\n"; |
| 40 | + } |
| 41 | +}); |
| 42 | + |
| 43 | +// Error event - handles connection and data errors |
| 44 | +$ticker->on('error', function(Exception $error) { |
| 45 | + echo "WebSocket Error: " . $error->getMessage() . "\n"; |
| 46 | +}); |
| 47 | + |
| 48 | +// Disconnection event - fires when connection is lost |
| 49 | +$ticker->on('disconnect', function(int $code, string $reason) { |
| 50 | + echo "WebSocket Disconnected: [{$code}] {$reason}\n"; |
| 51 | +}); |
| 52 | + |
| 53 | +// Connection close event - fires when connection closes gracefully |
| 54 | +$ticker->on('close', function(int $code, string $reason) { |
| 55 | + echo "WebSocket Connection Closed: [{$code}] {$reason}\n"; |
| 56 | +}); |
| 57 | + |
| 58 | +// Reconnection attempt event - fires during auto-reconnect |
| 59 | +$ticker->on('reconnect', function(int $attempt) { |
| 60 | + echo "Attempting to reconnect... (Attempt #{$attempt})\n"; |
| 61 | +}); |
| 62 | + |
| 63 | +// Max reconnection attempts reached |
| 64 | +$ticker->on('noreconnect', function() { |
| 65 | + echo "Maximum reconnection attempts reached. Connection abandoned.\n"; |
| 66 | +}); |
| 67 | + |
| 68 | +// Raw message event - receives all WebSocket messages |
| 69 | +$ticker->on('message', function(string $payload, bool $isBinary) { |
| 70 | + if ($isBinary) { |
| 71 | + echo "Binary message received (" . strlen($payload) . " bytes)\n"; |
| 72 | + } else { |
| 73 | + echo "Text message received: " . $payload . "...\n"; |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +// Order update event - receives order status updates |
| 78 | +$ticker->on('order_update', function(array $orderUpdate) { |
| 79 | + echo "Order Update Received:\n"; |
| 80 | + echo "Order ID: {$orderUpdate['order_id']} | Status: {$orderUpdate['status']} | Price: {$orderUpdate['price']} | Quantity: {$orderUpdate['quantity']}\n"; |
| 81 | +}); |
| 82 | + |
| 83 | +echo "Starting KiteTicker WebSocket connection...\n"; |
| 84 | + |
| 85 | +// Start the WebSocket connection |
| 86 | +$ticker->connect(); |
0 commit comments