title: How to force ActionCable to disconnect all clients when deploying new code to Heroku
body: <p>I have an application that stores a count of all connections to an actioncable channel, incrementing it on <code>subscribed</code>, and decrementing it on <code>unsubscribed</code>. However I've found an issue where deploying to Heroku won't <code>unsubscribed</code> the active connections when the server (Puma) goes down. Thus when the new version of the application spins up, the connection count is higher than what it should be.</p>
  
  <p>Code for my particular channel:</p>
  
  <pre><code>class PostChannel &lt; ApplicationCable::Channel
    attr_reader :subscribers
  
    def subscribed
      channel_name = "TestChannel#{params[:post_id]}"
      stream_from channel_name
      user_ids = active_users channel_name
      user_ids &lt;&lt; connection.current_user.id
      update_users channel_name, user_ids
    end
  
    def unsubscribed
      channel_name = "TestChannel#{params[:post_id]}"
      user_ids = active_users channel_name
      user_ids.delete_at(user_ids.index(connection.current_user.id) ||
                         user_ids.length)
      update_users channel_name, user_ids
    end
  
    def active_users(channel_name)
      JSON.parse(Redis.current.hget('actioncable', channel_name) || '[]')
    end
  
    def update_users(channel_name, user_ids)
      Redis.current.hset('actioncable', channel_name, user_ids.to_json)
      ActionCable.server.broadcast(
        channel_name,
        users: user_ids,
        action: 'UsersChanged'
      )
    end
  end
  </code></pre>
  
  <p>How do I force ActionCable to unsubscribe all active connections on a deployment to Heroku?</p>