Class: Bitmex::Websocket

Inherits:
Object
  • Object
show all
Defined in:
lib/bitmex/websocket.rb

Overview

Websocket API support www.bitmex.com/app/wsAPI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, api_key: nil, api_secret: nil) ⇒ Bitmex::Websocket

Create new websocket instance

Parameters:

  • host (String)

    the underlying host to connect to

  • api_key (String) (defaults to: nil)

    the api key

  • api_secret (String) (defaults to: nil)

    the api secret



15
16
17
18
19
20
# File 'lib/bitmex/websocket.rb', line 15

def initialize(host, api_key: nil, api_secret: nil)
  @host = host
  @api_key = api_key
  @api_secret = api_secret

end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/bitmex/websocket.rb', line 8

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



8
9
10
# File 'lib/bitmex/websocket.rb', line 8

def api_secret
  @api_secret
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/bitmex/websocket.rb', line 8

def host
  @host
end

Instance Method Details

#listen(topics) {|data, result| ... } ⇒ Object

Listen to generic topics

Parameters:

  • topics (Hash)

    topics to listen to e.g. { trade: “XBTUSD” }

Yield Parameters:

  • data (Hash)

    pushed via websocket

  • result (any)

    state of the previous block execution if any



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bitmex/websocket.rb', line 49

def listen(topics, &ablock)
  EM.run do
    connect

    topics.each do |topic, symbol|
      symbols = [symbol] if symbol.nil? || symbol.is_a?(String)
      symbols ||= symbol
      symbols.each do |symbol|
        subscribe topic, symbol, &ablock
      end
    end
  end
end

#stopObject

Stop websocket listener



64
65
66
# File 'lib/bitmex/websocket.rb', line 64

def stop
  EM.stop_event_loop
end

#subscribe(topic, symbol = nil, auth: false) {|Array| ... } ⇒ Object

Subscribe to a specific topic and optionally filter by symbol

Parameters:

  • topic (String)

    topic to subscribe to e.g. ‘trade’

  • symbol (String, Array) (defaults to: nil)

    symbol or symbols to filter

Yields:

  • (Array)

    data payload



26
27
28
29
30
31
32
33
# File 'lib/bitmex/websocket.rb', line 26

def subscribe(topic, symbol = nil, auth: false, &callback)
  raise 'callback block is required' unless block_given?

  @callbacks[topic.to_s] = callback

  payload = { op: :subscribe, args: [subscription(topic, symbol)] }
  @faye.send payload.to_json.to_s
end

#unsubscribe(topic, symbol = nil) ⇒ Object

Unsubscribe from a specific topic and symbol

Parameters:

  • topic (String)

    topic to subscribe to e.g. ‘trade’

  • symbol (String, Array) (defaults to: nil)

    symbol or symbols to filter



38
39
40
41
42
43
# File 'lib/bitmex/websocket.rb', line 38

def unsubscribe(topic, symbol = nil)
  @callbacks[topic.to_s] = nil

  payload = { op: :unsubscribe, args: [subscription(topic, symbol)] }
  @faye.send payload.to_json.to_s
end