Class: Binance::Client::WebSocket

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

Overview

Public: Client with methods mirroring the Binance WebSocket API

Constant Summary collapse

BASE_URL =

Public: String base url for WebSocket client to use

'wss://stream.binance.com:9443'.freeze

Instance Method Summary collapse

Instance Method Details

#agg_trade(symbol:, methods:) ⇒ Object

Public: Create an Aggregate Trade stream

:symbol - The String symbol the stream will listen to

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


61
62
63
# File 'lib/binance/client/websocket.rb', line 61

def agg_trade(symbol:, methods:)
  single stream: { symbol: symbol, type: 'aggTrade' }, methods: methods
end

#all_market_ticker(methods:) ⇒ Object

Public: Create a Ticker stream for all symbols

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


120
121
122
# File 'lib/binance/client/websocket.rb', line 120

def all_market_ticker(methods:)
  single stream: { symbol: '!ticker', type: 'arr' }, methods: methods
end

#diff_depth(symbol:, methods:) ⇒ Object

Public: Create a Diff Depth stream

:symbol - The String symbol the stream will listen to

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


152
153
154
# File 'lib/binance/client/websocket.rb', line 152

def diff_depth(symbol:, methods:)
  single stream: { symbol: symbol, type: 'depth' }, methods: methods
end

#kline(symbol:, interval:, methods:) ⇒ Object

Public: Create an Kline stream

:symbol - The String symbol the stream will listen to

:interval - The String interval the stream will update with. The

intervals that may be used can be found in the Binance API
docs.

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


93
94
95
96
# File 'lib/binance/client/websocket.rb', line 93

def kline(symbol:, interval:, methods:)
  single stream: { symbol: symbol, type: 'kline', interval: interval },
         methods: methods
end

#multi(streams:, methods:) ⇒ Object

Public: Create multiple WebSocket streams

:streams - The Array of Hashes used to define the stream. Each Hash can

         have the following keys:
:symbol   - The String symbol the stream will listen to
:type     - The String type of stream to listen to
:level    - The String level to use for the depth stream (optional)
:interval - The String interval to use for the kline stream (optional)

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


45
46
47
48
49
# File 'lib/binance/client/websocket.rb', line 45

def multi(streams:, methods:)
  names = streams.map { |stream| stream_url(stream) }
  create_stream("#{BASE_URL}/stream?streams=#{names.join('/')}",
                methods: methods)
end

#partial_book_depth(symbol:, level:, methods:) ⇒ Object

Public: Create an Partial Book Depth stream

:symbol - The String symbol the stream will listen to

:level - The String interval the stream will update with. The intervals

that may be used can be found in the Binance API docs.

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


137
138
139
140
# File 'lib/binance/client/websocket.rb', line 137

def partial_book_depth(symbol:, level:, methods:)
  single stream: { symbol: symbol, type: 'depth', level: level },
         methods: methods
end

#single(stream:, methods:) ⇒ Object

Public: Create a single WebSocket stream

:stream - The Hash used to define the stream

:symbol   - The String symbol to listen to
:type     - The String type of stream to listen to
:level    - The String level to use for the depth stream (optional)
:interval - The String interval to use for the kline stream (optional)

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


25
26
27
28
# File 'lib/binance/client/websocket.rb', line 25

def single(stream:, methods:)
  create_stream("#{BASE_URL}/ws/#{stream_url(stream)}",
                methods: methods)
end

#ticker(symbol:, methods:) ⇒ Object

Public: Create a Ticker stream

:symbol - The String symbol the stream will listen to

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


108
109
110
# File 'lib/binance/client/websocket.rb', line 108

def ticker(symbol:, methods:)
  single stream: { symbol: symbol, type: 'ticker' }, methods: methods
end

#trade(symbol:, methods:) ⇒ Object

Public: Create a Trade stream

:symbol - The String symbol the stream will listen to

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


75
76
77
# File 'lib/binance/client/websocket.rb', line 75

def trade(symbol:, methods:)
  single stream: { symbol: symbol, type: 'trade' }, methods: methods
end

#user_data(listen_key:, methods:) ⇒ Object

Public: Create a User Data stream

listen_key - The String key the stream will listen to, attained by

interacting with the REST API userDataStream endpoint

:methods - The Hash which contains the event handler methods to pass to

         the WebSocket client
:open    - The Proc called when a stream is opened (optional)
:message - The Proc called when a stream receives a message
:error   - The Proc called when a stream receives an error (optional)
:close   - The Proc called when a stream is closed (optional)


167
168
169
# File 'lib/binance/client/websocket.rb', line 167

def user_data(listen_key:, methods:)
  create_stream "#{BASE_URL}/ws/#{listen_key}", methods: methods
end