Class: Daytona::SocketIOClient

Inherits:
Object
  • Object
show all
Defined in:
lib/daytona/common/socketio_client.rb

Overview

Minimal Engine.IO/Socket.IO v4 client over raw WebSocket. Supports connect with auth, heartbeat, and event reception.

Engine.IO v4 heartbeat protocol (WebSocket transport):

- Server sends PING (type 2) every pingInterval ms
- Client must respond with PONG (type 3) within pingTimeout ms
- Client monitors for missing server PINGs to detect dead connections

Constant Summary collapse

EIO_OPEN =

Engine.IO v4 packet types

'0'
EIO_CLOSE =
'1'
EIO_PING =
'2'
EIO_PONG =
'3'
EIO_MESSAGE =
'4'
SIO_CONNECT =

Socket.IO v4 packet types (inside Engine.IO messages)

'0'
SIO_DISCONNECT =
'1'
SIO_EVENT =
'2'
SIO_CONNECT_ERROR =
'4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url:, token:, organization_id: nil, source: nil, sdk_version: nil, on_event: nil, on_disconnect: nil, connect_timeout: 5) ⇒ SocketIOClient

Returns a new instance of SocketIOClient.

Parameters:

  • api_url (String)

    The API URL (e.g., "https://app.daytona.io/api")

  • token (String)

    Auth token (API key or JWT)

  • organization_id (String, nil) (defaults to: nil)

    Organization ID for room joining

  • on_event (Proc) (defaults to: nil)

    Called with (event_name, data_hash) for each Socket.IO event

  • on_disconnect (Proc) (defaults to: nil)

    Called when the connection is lost

  • connect_timeout (Numeric) (defaults to: 5)

    Connection timeout in seconds



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/daytona/common/socketio_client.rb', line 42

def initialize(api_url:, token:, organization_id: nil, source: nil, sdk_version: nil, on_event: nil,
               on_disconnect: nil, connect_timeout: 5)
  @api_url = api_url
  @token = token
  @organization_id = organization_id
  @source = source
  @sdk_version = sdk_version
  @on_event = on_event
  @on_disconnect = on_disconnect
  @connect_timeout = connect_timeout
  @connected = false
  @mutex = Mutex.new
  @write_mutex = Mutex.new
  @health_thread = nil
  @ping_interval = 25
  @ping_timeout = 20
  @last_server_activity = Time.now
  @ws = nil
  @close_requested = false
end

Instance Attribute Details

#connectedObject (readonly)

Returns the value of attribute connected.



34
35
36
# File 'lib/daytona/common/socketio_client.rb', line 34

def connected
  @connected
end

Instance Method Details

#closeObject

Gracefully close the connection.



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/daytona/common/socketio_client.rb', line 130

def close
  @close_requested = true
  @health_thread&.kill
  @health_thread = nil

  send_raw(EIO_CLOSE) if @ws
  @ws&.close
  @mutex.synchronize { @connected = false }
rescue StandardError
  # Ignore errors during close
end

#connectBoolean

Establish the WebSocket connection and perform Socket.IO handshake.

Returns:

  • (Boolean)

    true if connection succeeded

Raises:

  • (StandardError)

    on connection failure



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/daytona/common/socketio_client.rb', line 66

def connect
  ws_url = build_ws_url
  connected_queue = Queue.new

  # Capture self because websocket-client-simple uses instance_exec for callbacks
  client = self

  # Register callbacks inside the connect block so they are installed before the
  # socket dials — otherwise the Engine.IO handshake can arrive before :message
  # is bound, stalling the connection into a polling fallback/timeout.
  @ws = WebSocket::Client::Simple.connect(ws_url) do |ws|
    ws.on :message do |msg|
      client.send(:handle_raw_message, msg.data.to_s, connected_queue)
    end

    ws.on :error do |_e|
      was_connected = client.instance_variable_get(:@mutex).synchronize do
        prev = client.instance_variable_get(:@connected)
        client.instance_variable_set(:@connected, false)
        prev
      end
      connected_queue.push(:error) unless was_connected
    end

    ws.on :close do
      mutex = client.instance_variable_get(:@mutex)
      was_connected = mutex.synchronize do
        prev = client.instance_variable_get(:@connected)
        client.instance_variable_set(:@connected, false)
        prev
      end
      on_disconnect = client.instance_variable_get(:@on_disconnect)
      close_requested = client.instance_variable_get(:@close_requested)
      begin
        on_disconnect&.call if was_connected && !close_requested
      rescue StandardError
        nil
      end
    end
  end

  # Wait for connection with timeout
  result = nil
  begin
    Timeout.timeout(@connect_timeout) { result = connected_queue.pop }
  rescue Timeout::Error
    close
    raise "WebSocket connection timed out after #{@connect_timeout}s"
  end

  if result != :connected
    close
    raise "WebSocket connection failed: #{result}"
  end

  @mutex.synchronize { @connected }
end

#connected?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/daytona/common/socketio_client.rb', line 125

def connected?
  @mutex.synchronize { @connected }
end