Module: DhanHQ::WS

Defined in:
lib/DhanHQ/ws.rb,
lib/DhanHQ/ws/client.rb,
lib/DhanHQ/ws/orders.rb,
lib/DhanHQ/ws/cmd_bus.rb,
lib/DhanHQ/ws/decoder.rb,
lib/DhanHQ/ws/registry.rb,
lib/DhanHQ/ws/segments.rb,
lib/DhanHQ/ws/sub_state.rb,
lib/DhanHQ/ws/connection.rb,
lib/DhanHQ/ws/market_depth.rb,
lib/DhanHQ/ws/orders/client.rb,
lib/DhanHQ/ws/packets/header.rb,
lib/DhanHQ/ws/singleton_lock.rb,
lib/DhanHQ/ws/base_connection.rb,
lib/DhanHQ/ws/orders/connection.rb,
lib/DhanHQ/ws/packets/oi_packet.rb,
lib/DhanHQ/ws/market_depth/client.rb,
lib/DhanHQ/ws/packets/full_packet.rb,
lib/DhanHQ/ws/market_depth/decoder.rb,
lib/DhanHQ/ws/packets/index_packet.rb,
lib/DhanHQ/ws/packets/quote_packet.rb,
lib/DhanHQ/ws/packets/ticker_packet.rb,
lib/DhanHQ/ws/websocket_packet_parser.rb,
lib/DhanHQ/ws/packets/disconnect_packet.rb,
lib/DhanHQ/ws/packets/prev_close_packet.rb,
lib/DhanHQ/ws/packets/depth_delta_packet.rb,
lib/DhanHQ/ws/packets/market_depth_level.rb,
lib/DhanHQ/ws/packets/market_status_packet.rb

Overview

WebSocket registry for managing connections and subscriptions

Defined Under Namespace

Modules: MarketDepth, Orders, Packets, Segments Classes: BaseConnection, Client, CmdBus, Connection, Decoder, Registry, SingletonLock, SubState, WebsocketPacketParser

Constant Summary collapse

DEBUG_FRAME_MAX_BYTES =

Cap on how many bytes of a frame get hex-dumped by debug_frame. A full depth packet can run several KB; at tick frequency that floods the log long before it adds diagnostic value beyond the first couple hundred bytes (header + the first few fields is normally enough to spot a parsing bug). The full frame size is always logged regardless of the cap.

256

Class Method Summary collapse

Class Method Details

.connect(mode: :ticker) {|tick| ... } ⇒ DhanHQ::WS::Client

Establishes a WebSocket connection and yields decoded ticks.

Examples:

Subscribe to ticker updates

DhanHQ::WS.connect(mode: :ticker) do |tick|
  puts tick.inspect
end

Parameters:

  • mode (Symbol) (defaults to: :ticker)

    Desired feed mode (:ticker, :quote, :full).

Yields:

  • (tick)

Yield Parameters:

  • tick (Hash)

    A decoded tick emitted by the streaming API.

Returns:



24
25
26
# File 'lib/DhanHQ/ws.rb', line 24

def self.connect(mode: :ticker, &on_tick)
  Client.new(mode: mode).start.on(:tick, &on_tick)
end

.debug_frame(source, data) ⇒ void

This method returns an undefined value.

Logs a raw inbound WebSocket frame as a hex dump when config.ws_debug (+DHAN_WS_DEBUG=true+) is enabled. A no-op otherwise -- the flag is checked before any hex encoding work, so there's no cost when debug logging is off. Frames longer than DEBUG_FRAME_MAX_BYTES are truncated in the dump, but the logged byte count is always the full frame size.

Parameters:

  • source (String)

    short tag identifying which connection the frame came from

  • data (String)

    raw frame bytes



55
56
57
58
59
60
61
# File 'lib/DhanHQ/ws.rb', line 55

def self.debug_frame(source, data)
  return unless DhanHQ.configuration&.ws_debug?

  hex = data.byteslice(0, DEBUG_FRAME_MAX_BYTES).unpack1("H*")
  hex += "...truncated" if data.bytesize > DEBUG_FRAME_MAX_BYTES
  DhanHQ.logger&.debug("[DhanHQ::WS::#{source}] frame (#{data.bytesize} bytes): #{hex}")
end

.disconnect_all_local!Object

convenience API



34
35
36
# File 'lib/DhanHQ/ws.rb', line 34

def self.disconnect_all_local!
  Registry.stop_all
end