'Unable to detect the client activation status in server side if disconnected due to poor network - Action cable

We facing issues with actioncable. The issue is , Client is getting disconnected due to internet issue but that status not communicated to the server(disconnect event not calling)

The below method is failing due to inactive client,

ActionCable.server.broadcast "project_123_channel", {
      "request" => "show_title"
    }

Ruby version:2.5.3p105 Rails version:5.2.3 actin cable version: 5.2.3

disconnect callback:

App.live_data = App.cable.subscriptions.create({ channel: "ProjectDataChannel", project_id: project_id }, {
    collection: function() {
    },
    connected: function() {
     console.log("Live data connected");
    },
    disconnected: function() {
      console.log("disconnected");
      return this.perform('unfollow');
    }

But this disconnect callback not working.

Is there any way to detect the client active status from server side? or there any callback to track the disconnect status if the client is disconnect due to internet kind of things



Solution 1:[1]

This is a known issue.

Here is a blog post from Vlad Dyachenko that describes the problem and a solution...

https://wowinter13.medium.com/actioncable-handling-client-connection-errors-on-server-side-93ea74178d03

This is the code solution from the post...

module ApplicationCable
  class Channel < ActionCable::Channel::Base
    after_subscribe :connection_monitor
    CONNECTION_TIMEOUT = 10.seconds
    CONNECTION_PING_INTERVAL = 5.seconds
    periodically every: CONNECTION_PING_INTERVAL do
      @driver&.ping
      if Time.now - @_last_request_at > @_timeout
        connection.disconnect
      end
    end
    def connection_monitor
      @_last_request_at ||= Time.now
      @_timeout = CONNECTION_TIMEOUT
      @driver = connection.instance_variable_get('@websocket').possible?&.instance_variable_get('@driver')
      @driver.on(:pong) { @_last_request_at = Time.now }
    end
  end
end

It's worked fine as suggestion given by @dbugger

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dbugger