Class: SolanaRuby::WebSocketClient

Constant Summary

Constants included from SolanaRuby::WebSocketMethods::SignatureMethods

SolanaRuby::WebSocketMethods::SignatureMethods::BASE_64_ENCODING_OPTIONS, SolanaRuby::WebSocketMethods::SignatureMethods::FINALIZED_OPTIONS

Constants included from SolanaRuby::WebSocketMethods::AccountMethods

SolanaRuby::WebSocketMethods::AccountMethods::ENCODING_OPTIONS, SolanaRuby::WebSocketMethods::AccountMethods::FINALIZED_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SolanaRuby::WebSocketMethods::SlotMethods

#on_slot_change, #remove_slot_change_listener

Methods included from SolanaRuby::WebSocketMethods::RootMethods

#on_root_change, #remove_root_listener

Methods included from SolanaRuby::WebSocketMethods::SignatureMethods

#on_signature, #on_signature_with_options, #remove_signature_listener

Methods included from SolanaRuby::WebSocketMethods::LogMethods

#on_logs, #on_logs_for_account, #on_logs_for_program, #remove_logs_listener

Methods included from SolanaRuby::WebSocketMethods::AccountMethods

#on_account_change, #on_program_account_change, #remove_account_change_listener, #remove_program_account_listener

Methods included from WebSocketHandlers

#setup_handlers

Constructor Details

#initialize(url, auto_reconnect: true, reconnect_delay: 5) ⇒ WebSocketClient

Returns a new instance of WebSocketClient.



20
21
22
23
24
25
26
27
28
# File 'lib/solana_ruby/web_socket_client.rb', line 20

def initialize(url, auto_reconnect: true, reconnect_delay: 5)
  @url = url
  @subscriptions = {}
  @auto_reconnect = auto_reconnect
  @reconnect_delay = reconnect_delay
  @connected = false
  @ws = nil
  connect
end

Instance Attribute Details

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



18
19
20
# File 'lib/solana_ruby/web_socket_client.rb', line 18

def subscriptions
  @subscriptions
end

Instance Method Details

#attempt_reconnectObject



85
86
87
88
89
90
91
92
# File 'lib/solana_ruby/web_socket_client.rb', line 85

def attempt_reconnect
  return unless @auto_reconnect

  puts "Attempting to reconnect in #{@reconnect_delay} seconds..."
  @connected = false
  sleep @reconnect_delay
  reconnect
end

#connectObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/solana_ruby/web_socket_client.rb', line 30

def connect
  return if @connected

  # Close existing WebSocket if it exists
  @ws&.close if @ws

  @ws = WebSocket::Client::Simple.connect(@url)
  setup_handlers(@ws, self)
  @connected = true
rescue StandardError => e
  puts "Failed to connect: #{e.message}"
  attempt_reconnect
end

#handle_message(data) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/solana_ruby/web_socket_client.rb', line 73

def handle_message(data)
  if data['id'] && @subscriptions[data['id']]
    @subscriptions[data['id']].call(data['result'])
  elsif data['method'] && data['params']
    @subscriptions.each do |id, block|
      block.call(data['params']) if block
    end
  else
    puts "Unhandled message: #{data}"
  end
end

#reconnectObject



44
45
46
47
# File 'lib/solana_ruby/web_socket_client.rb', line 44

def reconnect
  @connected = false
  connect
end

#subscribe(method, params = nil, &block) ⇒ Object



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

def subscribe(method, params = nil, &block)
  id = SecureRandom.uuid
  @subscriptions[id] = block
  message = {
    jsonrpc: '2.0',
    id: id,
    method: method
  }
  message[:params] = params if params
  sleep 2
  @ws.send(message.to_json)
  id
end

#unsubscribe(method, subscription_id) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/solana_ruby/web_socket_client.rb', line 63

def unsubscribe(method, subscription_id)
  message = {
    jsonrpc: '2.0',
    id: SecureRandom.uuid,
    method: method,
    params: [subscription_id]
  }
  @ws.send(message.to_json)
end