Add an online
field to Users
class AddOnlineToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :online, :boolean, default: false
end
end
Make an appearance channel
class AppearanceChannel < ApplicationCable::Channel
def subscribed
stream_from "appearance_channel"
if current_user
ActionCable.server.broadcast "appearance_channel", { user: current_user.id, online: :on }
current_user.online = true
current_user.save!
end
end
def unsubscribed
if current_user
# Any cleanup needed when channel is unsubscribed
ActionCable.server.broadcast "appearance_channel", { user: current_user.id, online: :off }
current_user.online = false
current_user.save!
end
end
end
Make sure all your visitors subscribe to the AppearanceChannel
when they enter your site (via some JavaScript call, see http://guides.rubyonrails.org/action_cable_overview.html#client-side-components ). Add the authorisation to Action Cable:
Like this https://rubytutorial.io/actioncable-devise-authentication/
or like that How To use devise_token_auth with ActionCable for authenticating user?
Apply again some JavaScript code to detect the incoming "{ user: current_user.id, online: :on }" message and set the green dot on a user avatar.