Module: Pnut::AppStream

Extended by:
AppStream
Included in:
AppStream
Defined in:
lib/pnut/app_stream.rb

Instance Method Summary collapse

Instance Method Details

#start(access_token: nil, stream_key: nil, handler: method(:puts)) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pnut/app_stream.rb', line 10

def start(access_token: nil, stream_key: nil, handler: method(:puts))
  EM.run do
    ws = Faye::WebSocket::Client.new(
      "wss://stream.pnut.io/v0/app/stream?access_token=#{access_token}&key=#{stream_key}"
    )

    EM.add_periodic_timer 45 do
      ws.ping "ping"
    end

    ws.on :open do |event|
      p [:open]
      ws.send("Hello, pnut!")
    end

    ws.on :message do |event|
      handler.call(event.data)
    end

    ws.on :close do |event|
      p [:close, event.code, event.reason]

      ws = nil
    end
  end
end