Class: DhanHQ::WS::Orders::Client

Inherits:
Object
  • Object
show all
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

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, **options)
  @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 = options
end

Instance Method Details

#all_ordersHash

Get all tracked orders

Returns:

  • (Hash)

    Hash of order_no => OrderUpdate



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

Returns:

  • (Boolean)

    true if connected



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_ordersArray<OrderUpdate>

Get fully executed orders

Returns:

  • (Array<OrderUpdate>)

    Orders that are fully executed



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

Parameters:

  • event (Symbol)

    Event type (:update, :raw, :status_change, :execution, :error)

  • block (Proc)

    Event handler

Returns:

  • (Client)

    self for method chaining



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

Parameters:

  • order_no (String)

    Order number

Returns:

  • (OrderUpdate, nil)

    Latest order update or nil if not found



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

Parameters:

  • status (String)

    Order status (TRANSIT, PENDING, REJECTED, etc.)

Returns:

  • (Array<OrderUpdate>)

    Orders with the specified 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

Parameters:

  • symbol (String)

    Trading symbol

Returns:

  • (Array<OrderUpdate>)

    Orders for the specified 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_ordersArray<OrderUpdate>

Get partially executed orders

Returns:

  • (Array<OrderUpdate>)

    Orders that are partially executed



113
114
115
# File 'lib/DhanHQ/ws/orders/client.rb', line 113

def partially_executed_orders
  @order_tracker.values.select(&:partially_executed?)
end

#pending_ordersArray<OrderUpdate>

Get pending orders (not executed)

Returns:

  • (Array<OrderUpdate>)

    Orders that are not executed



127
128
129
# File 'lib/DhanHQ/ws/orders/client.rb', line 127

def pending_orders
  @order_tracker.values.select(&:not_executed?)
end

#startClient

Start the WebSocket connection and begin receiving order updates

Returns:

  • (Client)

    self for method chaining



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| handle_message(msg) }
  @conn.start
  start_cleanup_thread
  DhanHQ::WS::Registry.register(self) if defined?(DhanHQ::WS::Registry)
  self
end

#stopvoid

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