class RTCBX
This class represents the current state of the CoinBase Exchange orderbook.
Orderbook
version number. I try to keep it semantic.
Constants
- PING_INTERVAL
seconds in between pinging the connection.
- VERSION
Attributes
The Coinbase Pro Client object You can use this if you need to make API calls
Epoch time indicating the last time we received a pong from Coinbase Pro in response to one of our pings
An array of blocks to be run each time a message comes in on the Websocket
The Coinbase Pro product being tracked (eg. “BTC-USD”)
The message queue from the Websocket. The websocket_thread
processes this queue
Boolean, whether the orderbook goes live on creation or not If false
, #start!
must be called to initiate tracking.
The Websocket object
The thread that consumes the websocket data
Public Class Methods
Create a new RTCBX
object with options and an optional block to be run when each message is called.
Generally you won't call this directly. You'll use RTCBX::Orderbook.new
, RTCBX::Trader.new
, or RTCBX::Candles.new
.
You can also subclass RTCBX
and call this method through super
, as the classes mentioned above do.
RTCBX
handles connecting to the Websocket, setting up the client, and managing the thread that consumes the Websocket feed.
# File lib/rtcbx.rb, line 59 def initialize(options = {}, &block) @product_id = options.fetch(:product_id, 'BTC-USD') @start = options.fetch(:start, true) @api_key = options.fetch(:api_key, '') @api_secret = options.fetch(:api_secret, '') @api_passphrase = options.fetch(:api_passphrase, '') @message_callbacks = [] @message_callbacks << block if block_given? @client = Coinbase::Exchange::Client.new( api_key, api_secret, api_passphrase, product_id: product_id ) @websocket = Coinbase::Exchange::Websocket.new( keepalive: true, product_id: product_id ) @queue = Queue.new start! if start end
Public Instance Methods
Stops, then starts the thread that consumes the Websocket feed
# File lib/rtcbx.rb, line 93 def reset! stop! start! end
Starts the thread to consume the Websocket feed
# File lib/rtcbx.rb, line 82 def start! start_websocket_thread end
Stops the thread and disconnects from the Websocket
# File lib/rtcbx.rb, line 87 def stop! websocket_thread.kill websocket.stop! end
Private Instance Methods
Configures the Websocket object to print any errors to the console
# File lib/rtcbx.rb, line 134 def setup_error_handler EM.error_handler do |e| print "Websocket Error: #{e.message} - #{e.backtrace.join("\n")}" end end
Configures the websocket to periodically ping Coinbase Pro and confirm connection
# File lib/rtcbx.rb, line 125 def setup_ping_timer EM.add_periodic_timer(PING_INTERVAL) do websocket.ping do @last_pong = Time.now end end end
Configures the websocket to pass each message to each of the defined message callbacks
# File lib/rtcbx.rb, line 105 def setup_websocket_callback websocket.message do |message| queue.push(message) message_callbacks.each { |b| b&.call(message) } end end
Starts the thread that consumes the websocket
# File lib/rtcbx.rb, line 113 def start_websocket_thread @websocket_thread = Thread.new do setup_websocket_callback EM.run do websocket.start! setup_ping_timer setup_error_handler end end end