Class: DhanHQ::WS::Orders::Client
- Inherits:
-
Object
- Object
- DhanHQ::WS::Orders::Client
- Defined in:
- lib/DhanHQ/ws/orders/client.rb
Overview
Enhanced WebSocket client for real-time order updates Provides comprehensive order state tracking and event handling
Constant Summary collapse
- MAX_TRACKED_ORDERS =
Maximum number of orders to keep in tracker (default: 10,000)
ENV.fetch("DHAN_WS_MAX_TRACKED_ORDERS", 10_000).to_i
- MAX_ORDER_AGE =
Maximum age of orders in tracker in seconds (default: 7 days)
ENV.fetch("DHAN_WS_MAX_ORDER_AGE", 604_800).to_i
Instance Method Summary collapse
-
#all_orders ⇒ Hash
Get all tracked orders.
-
#connected? ⇒ Boolean
Check if connection is active.
-
#disconnect! ⇒ void
Force disconnect the WebSocket.
-
#fully_executed_orders ⇒ Array<OrderUpdate>
Get fully executed orders.
-
#initialize(url: nil, **options) ⇒ Client
constructor
A new instance of Client.
-
#on(event, &block) ⇒ Client
Register event handlers.
-
#order_state(order_no) ⇒ OrderUpdate?
Get current order state for a specific order.
-
#orders_by_status(status) ⇒ Array<OrderUpdate>
Get orders by status.
-
#orders_by_symbol(symbol) ⇒ Array<OrderUpdate>
Get orders by symbol.
-
#partially_executed_orders ⇒ Array<OrderUpdate>
Get partially executed orders.
-
#pending_orders ⇒ Array<OrderUpdate>
Get pending orders (not executed).
-
#start ⇒ Client
Start the WebSocket connection and begin receiving order updates.
-
#stop ⇒ void
Stop the WebSocket connection.
Constructor Details
#initialize(url: nil, **options) ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 19 def initialize(url: nil, **) @callbacks = Concurrent::Map.new { |h, k| h[k] = [] } @started = Concurrent::AtomicBoolean.new(false) @order_tracker = Concurrent::Map.new @order_timestamps = Concurrent::Map.new @cleanup_mutex = Mutex.new @cleanup_thread = nil cfg = DhanHQ.configuration @url = url || cfg.ws_order_url @connection_options = end |
Instance Method Details
#all_orders ⇒ Hash
Get all tracked orders
90 91 92 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 90 def all_orders @order_tracker.dup end |
#connected? ⇒ Boolean
Check if connection is active
134 135 136 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 134 def connected? @conn&.open? || false end |
#disconnect! ⇒ void
This method returns an undefined value.
Force disconnect the WebSocket
65 66 67 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 65 def disconnect! @conn&.disconnect! end |
#fully_executed_orders ⇒ Array<OrderUpdate>
Get fully executed orders
120 121 122 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 120 def fully_executed_orders @order_tracker.values.select(&:fully_executed?) end |
#on(event, &block) ⇒ Client
Register event handlers
74 75 76 77 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 74 def on(event, &block) @callbacks[event] << block self end |
#order_state(order_no) ⇒ OrderUpdate?
Get current order state for a specific order
83 84 85 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 83 def order_state(order_no) @order_tracker[order_no] end |
#orders_by_status(status) ⇒ Array<OrderUpdate>
Get orders by status
98 99 100 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 98 def orders_by_status(status) @order_tracker.values.select { |order| order.status == status } end |
#orders_by_symbol(symbol) ⇒ Array<OrderUpdate>
Get orders by symbol
106 107 108 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 106 def orders_by_symbol(symbol) @order_tracker.values.select { |order| order.symbol == symbol } end |
#partially_executed_orders ⇒ Array<OrderUpdate>
Get partially executed orders
113 114 115 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 113 def partially_executed_orders @order_tracker.values.select(&:partially_executed?) end |
#pending_orders ⇒ Array<OrderUpdate>
Get pending orders (not executed)
127 128 129 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 127 def pending_orders @order_tracker.values.select(&:not_executed?) end |
#start ⇒ Client
Start the WebSocket connection and begin receiving order updates
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 34 def start return self if @started.true? @started.make_true @conn = Connection.new(url: @url, **@connection_options) @conn.on(:open) { emit(:open, true) } @conn.on(:close) { |payload| emit(:close, payload) } @conn.on(:error) { |error| emit(:error, error) } @conn.on(:message) { |msg| (msg) } @conn.start start_cleanup_thread DhanHQ::WS::Registry.register(self) if defined?(DhanHQ::WS::Registry) self end |
#stop ⇒ void
This method returns an undefined value.
Stop the WebSocket connection
52 53 54 55 56 57 58 59 60 |
# File 'lib/DhanHQ/ws/orders/client.rb', line 52 def stop return unless @started.true? @started.make_false stop_cleanup_thread @conn&.stop emit(:close, true) DhanHQ::WS::Registry.unregister(self) if defined?(DhanHQ::WS::Registry) end |