Class: Phoenix::Socket

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/yatapp/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSocket

Returns a new instance of Socket.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yatapp/socket.rb', line 16

def initialize
  initialize_configuration
  @path = "wss://run.yatapp.net/socket/websocket?api_token=#{api_access_token}"
  @topic = "translations:#{project_id}"
  @join_options = {}
  @connect_options = {}
  @inbox = Phoenix::Inbox.new(ttl: 15)
  super() # MonitorMixin
  @inbox_cond = new_cond
  @thread_ready = new_cond
  @topic_cond = new_cond
  reset_state_conditions
  request_reply(event: "ping", payload: {})
end

Instance Attribute Details

#connect_options_procObject

Returns the value of attribute connect_options_proc.



13
14
15
# File 'lib/yatapp/socket.rb', line 13

def connect_options_proc
  @connect_options_proc
end

#inboxObject (readonly)

Returns the value of attribute inbox.



12
13
14
# File 'lib/yatapp/socket.rb', line 12

def inbox
  @inbox
end

#join_options_procObject

Returns the value of attribute join_options_proc.



13
14
15
# File 'lib/yatapp/socket.rb', line 13

def join_options_proc
  @join_options_proc
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/yatapp/socket.rb', line 12

def path
  @path
end

#socketObject (readonly)

Returns the value of attribute socket.



12
13
14
# File 'lib/yatapp/socket.rb', line 12

def socket
  @socket
end

#topicObject (readonly)

Returns the value of attribute topic.



12
13
14
# File 'lib/yatapp/socket.rb', line 12

def topic
  @topic
end

#verboseObject

Returns the value of attribute verbose.



13
14
15
# File 'lib/yatapp/socket.rb', line 13

def verbose
  @verbose
end

Instance Method Details

#connect_optionsObject



62
63
64
65
# File 'lib/yatapp/socket.rb', line 62

def connect_options
  return @connect_options unless connect_options_proc
  connect_options_proc.call(@connect_options)
end

#join_optionsObject



57
58
59
60
# File 'lib/yatapp/socket.rb', line 57

def join_options
  return @join_options unless join_options_proc
  join_options_proc.call(@join_options)
end

#request_reply(event:, payload: {}, timeout: 5) ⇒ Object

timeout in seconds



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/yatapp/socket.rb', line 31

def request_reply(event:, payload: {}, timeout: 5) # timeout in seconds
  ref = SecureRandom.uuid
  synchronize do
    ensure_connection
    @topic_cond.wait_until { @topic_joined }
    EM.next_tick { socket.send({ topic: topic, event: event, payload: payload, ref: ref }.to_json) }
    log [event, ref]

    # Ruby's condition variables only support timeout on the basic 'wait' method;
    # This should behave roughly as if wait_until also support a timeout:
    # `inbox_cond.wait_until(timeout) { inbox.key?(ref) || @dead }
    #
    # Note that this serves only to unblock the main thread, and should not halt execution of the
    # socket connection. Therefore, there is a possibility that the inbox may pile up with
    # unread messages if a lot of timeouts are encountered. A self-sweeping inbox will
    # be implemented to prevent this.
    ts = Time.now
    loop do
      inbox_cond.wait(timeout) # waits until time expires or signaled
      break if inbox.key?(ref) || @dead
      raise 'timeout' if timeout && Time.now > (ts + timeout)
    end
    inbox.delete(ref) { raise "reply #{ref} not found" }
  end
end