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

api_key[R]

API key used to authenticate to the API Not required for Orderbook or Candles

api_passphrase[R]
api_secret[R]
client[R]

The Coinbase Pro Client object You can use this if you need to make API calls

last_pong[R]

Epoch time indicating the last time we received a pong from Coinbase Pro in response to one of our pings

message_callbacks[R]

An array of blocks to be run each time a message comes in on the Websocket

product_id[R]

The Coinbase Pro product being tracked (eg. “BTC-USD”)

queue[R]

The message queue from the Websocket. The websocket_thread processes this queue

start[R]

Boolean, whether the orderbook goes live on creation or not If false, #start! must be called to initiate tracking.

websocket[R]

The Websocket object

websocket_thread[R]

The thread that consumes the websocket data

Public Class Methods

new(options = {}, &block) click to toggle source

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

reset!() click to toggle source

Stops, then starts the thread that consumes the Websocket feed

# File lib/rtcbx.rb, line 93
def reset!
  stop!
  start!
end
start!() click to toggle source

Starts the thread to consume the Websocket feed

# File lib/rtcbx.rb, line 82
def start!
  start_websocket_thread
end
stop!() click to toggle source

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

setup_error_handler() click to toggle source

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
setup_ping_timer() click to toggle source

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
setup_websocket_callback() click to toggle source

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
start_websocket_thread() click to toggle source

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