Module: Arc::WS

Defined in:
lib/architect/ws.rb

Class Method Summary collapse

Class Method Details

.send(id:, payload:) ⇒ Object

send a message to a web socket



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/architect/ws.rb', line 12

def self.send(id:, payload:)
  if ENV['NODE_ENV'] == 'testing'
    headers = {'content-type':'application/json'}
    uri = URI('https://localhost:3333/__arc')
    http = Net::HTTP.new(uri.host, uri.port)
    req = Net::HTTP::Post.new(uri.path, headers)
    req.body = payload.to_json
    http.request(req).read_body
  else
    arc = Arc.reflect
    url = arc['ws']['https']
    api = Aws::ApiGatewayManagementApi::Client.new({endpoint: url})
    api.postToConnection({
      connection_id: id,
      data: JSON.stringify(payload)
    })
  end
end