How to change ping interval in cable rails actions

I use ActionCable and get pings from the server every 3 seconds (mentioned in the ActionCable library). My question is: how can I change the ping interval during a subscription?

Any idea?

+6
source share
2 answers

@BoraMa answer extension:

You can override the constant on the server side as follows:

# config/initializers/action_cable.rb module ActionCable module Server module Connections BEAT_INTERVAL = 5 end end end 

On the client side, you also need to override the value:

 // this should be after //= require action_cable // but before any App.cable.subscriptions.create call // the value here *must* be 2 times the backend value ActionCable.ConnectionMonitor.staleThreshold = 10; 

Note that this approach is usually a very bad idea: messing around with internal variables is one of the direct ways to make mistakes and problems.

In fact, a ruby ​​will even warn you:

 config/initializers/action_cable.rb:7: warning: already initialized constant ActionCable::Server::Connections::BEAT_INTERVAL 

Use this only if you know what you are doing.

+6
source

Interestingly, with Rails 5.0.0.rc1, it seems that you cannot configure the ping interval . It is defined as a constant in ActionCable :: Server :: Connections module .

You might have redefined this constant in the initializer so that the server sends pings at different intervals, but it still would not help you in the end, because the client code that receives the ping also has a statically defined timeout (set to 6 seconds, i.e. two missed passes from the server). When it reaches the 6 second timeout without ping from the server, it tries to reconnect. And I'm not sure how you can override this constant in Javascript client code .

Judging by this github issue , some related debate continues about possible ways to improve pings behavior to be more useful, for example. given the network latency.

But essentially, the interval is not configurable at the moment, and if you don't want the ping interval less than 3 seconds, I see no easy way to override it in Rails now.

+2
source

All Articles