Class: DhanHQ::WS::MarketDepth::Client

Inherits:
BaseConnection show all
Defined in:
lib/DhanHQ/ws/market_depth/client.rb

Overview

WebSocket client for Full Market Depth data Provides real-time market depth (bid/ask levels) for specified symbols

Constant Summary collapse

SUBSCRIBE_REQUEST_CODE =
23
UNSUBSCRIBE_REQUEST_CODE =
12

Constants inherited from BaseConnection

BaseConnection::COOL_OFF_429, BaseConnection::MAX_BACKOFF

Instance Attribute Summary

Attributes inherited from BaseConnection

#callbacks, #started, #stopping, #url

Instance Method Summary collapse

Methods inherited from BaseConnection

#connected?, #disconnect!, #emit, #on, #open?, #send_message, #stop

Constructor Details

#initialize(symbols: [], **options) ⇒ Client

Initialize Market Depth WebSocket client

Parameters:

  • symbols (Array<String, Hash>) (defaults to: [])

    List of symbols (or metadata hashes) to subscribe to

  • options (Hash)

    Connection options



23
24
25
26
27
28
29
30
31
32
# File 'lib/DhanHQ/ws/market_depth/client.rb', line 23

def initialize(symbols: [], **options)
  cfg = DhanHQ.configuration
  url = options[:url] || build_market_depth_url(cfg)
  super(url: url, **options)

  @symbols = Array(symbols)
  @subscriptions = Concurrent::Map.new
  @instrument_cache = Concurrent::Map.new
  @decoder = Decoder.new
end

Instance Method Details

#startClient

Start the Market Depth WebSocket connection

Returns:

  • (Client)

    self for method chaining



37
38
39
40
41
# File 'lib/DhanHQ/ws/market_depth/client.rb', line 37

def start
  super
  subscribe_to_symbols(@symbols) if @symbols.any?
  self
end

#subscribe(symbols) ⇒ Client

Subscribe to market depth for specific symbols

Parameters:

  • symbols (Array<String, Hash>)

    Symbols (or metadata hashes) to subscribe to

Returns:

  • (Client)

    self for method chaining



47
48
49
50
# File 'lib/DhanHQ/ws/market_depth/client.rb', line 47

def subscribe(symbols)
  subscribe_to_symbols(Array(symbols))
  self
end

#subscribed?(symbol) ⇒ Boolean

Check if subscribed to a symbol

Parameters:

  • symbol (String, Hash)

    Symbol or metadata hash to check

Returns:

  • (Boolean)

    true if subscribed



72
73
74
# File 'lib/DhanHQ/ws/market_depth/client.rb', line 72

def subscribed?(symbol)
  @subscriptions.key?(normalize_label(symbol))
end

#subscriptionsArray<String>

Get current subscriptions

Returns:

  • (Array<String>)

    Currently subscribed symbol labels



64
65
66
# File 'lib/DhanHQ/ws/market_depth/client.rb', line 64

def subscriptions
  @subscriptions.values.map { |meta| meta[:original_label] }
end

#unsubscribe(symbols) ⇒ Client

Unsubscribe from market depth for specific symbols

Parameters:

  • symbols (Array<String, Hash>)

    Symbols (or metadata hashes) to unsubscribe from

Returns:

  • (Client)

    self for method chaining



56
57
58
59
# File 'lib/DhanHQ/ws/market_depth/client.rb', line 56

def unsubscribe(symbols)
  unsubscribe_from_symbols(Array(symbols))
  self
end