svoboda-jan / asterisk-ari

Ruby client library for the Asterisk REST Interface (ARI).
MIT License
41 stars 20 forks source link

[question] How should one reconnect? #8

Closed edmz closed 3 years ago

edmz commented 3 years ago

My server is running behind a NAT that appears to be causing a disconnect after N minutes of inactivity.

I thought it might be trivial to just reconnect on websocket error, like so:

client.on :websocket_error do |err|
  client.connect_websocket
end

But this doesn't seem to work. As far as I can tell, if I reconnect, the old listeners are deleted, which is undesirable.

Is there a recommended strategy to reconnect? If not, can you point me in the direction on what to change to be able to reconnect while keeping the already defined callbacks?

Regards

derekmwright commented 3 years ago

I know this is late but for any future searcher you can try implementing a keepalive:

  EventMachine.add_periodic_timer(15) do
    ping = @client.get('/asterisk/ping')
    @client.emit(:pong, ping)
  end

  @client.on :pong do |e|
    puts e
  end

Looks like we could use an updated generation of this client as swagger should have generated an attr for this @client.asterisk.ping

svoboda-jan commented 3 years ago

There is no recommended reconnect strategy, it depends on specific use case and is up to the user to implement one. Callbacks for events like :stasis_start shouldn't get deleted. Closing for now, please feel free to reopen if this is still an issue providing a minimal working example to reproduce it.