Class: ToggleCraft::SSEConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/togglecraft/sse_connection.rb

Overview

SSE (Server-Sent Events) connection manager Manages persistent connection to ToggleCraft SSE server with heartbeat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, sdk_key:, **options) ⇒ SSEConnection

Initialize SSE connection



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/togglecraft/sse_connection.rb', line 17

def initialize(url:, sdk_key:, **options)
  @url = url.sub(%r{/cable$}, '') # Remove /cable if present
  @sdk_key = sdk_key
  @options = {
    reconnect_interval: 1,
    max_reconnect_interval: 30,
    max_reconnect_attempts: 10,
    slow_reconnect_interval: 60,
    heartbeat_domain: 'https://togglecraft.io',
    heartbeat_interval: 300, # 5 minutes
    debug: false
  }.merge(options)

  @connected = false
  @connection_id = nil
  @reconnect_attempts = 0
  @should_reconnect = true

  # Callbacks
  @on_message = options[:on_message] || proc { }
  @on_connect = options[:on_connect] || proc { }
  @on_disconnect = options[:on_disconnect] || proc { }
  @on_error = options[:on_error] || proc { }

  # Threads
  @connection_thread = nil
  @heartbeat_thread = nil
  @heartbeat_timer = nil
end

Instance Attribute Details

#connectedObject (readonly)

Returns the value of attribute connected.



11
12
13
# File 'lib/togglecraft/sse_connection.rb', line 11

def connected
  @connected
end

#connection_idObject (readonly)

Returns the value of attribute connection_id.



11
12
13
# File 'lib/togglecraft/sse_connection.rb', line 11

def connection_id
  @connection_id
end

#sdk_keyObject (readonly)

Returns the value of attribute sdk_key.



11
12
13
# File 'lib/togglecraft/sse_connection.rb', line 11

def sdk_key
  @sdk_key
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/togglecraft/sse_connection.rb', line 11

def url
  @url
end

Instance Method Details

#connectvoid

This method returns an undefined value.

Connect to SSE server



49
50
51
52
53
54
# File 'lib/togglecraft/sse_connection.rb', line 49

def connect
  return if @connected

  @should_reconnect = true
  start_connection
end

#connected?Boolean

Check if connected to SSE server



78
79
80
# File 'lib/togglecraft/sse_connection.rb', line 78

def connected?
  @connected
end

#disconnectvoid

This method returns an undefined value.

Disconnect from SSE server



58
59
60
61
62
63
64
65
# File 'lib/togglecraft/sse_connection.rb', line 58

def disconnect
  log 'Disconnecting'
  @should_reconnect = false
  @connection_id = nil
  stop_heartbeat
  stop_connection
  @connected = false
end

#reconnectvoid

This method returns an undefined value.

Reconnect to SSE server



69
70
71
72
73
74
# File 'lib/togglecraft/sse_connection.rb', line 69

def reconnect
  disconnect
  @should_reconnect = true
  @reconnect_attempts = 0
  connect
end