1
私は、actioncableチャネルへのすべての接続の数を保存し、subscribed
にインクリメントし、unsubscribed
でデクリメントするアプリケーションを持っています。しかし、私は、サーバ(Puma)がダウンしたときに、Herokuへの展開がアクティブな接続ではないunsubscribed
という問題を発見しました。したがって、新しいバージョンのアプリケーションがスピンアップすると、接続数は必要な値よりも高くなります。Heokuに新しいコードをデプロイするときにActionCableがすべてのクライアントを切断する方法
私の特定のチャネルのためのコード:
class PostChannel < 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 << 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
どのように私はHerokuのに展開上のすべてのアクティブな接続を解除するActionCableを強制できますか?
これは私が考えていた解決策でした。私はそれにショット – JeremyKirkham
を与えますが、ページを更新しない限りユーザーは再接続しません。 –
@NicolasMaloeuvreこれは質問で尋ねられなかったので、それはできません。 –