If you're just trying to bootstrap a proof-of-concept application on your local workstation, you don't technically have to worry about giving ActionCable the ability to distinguish between multiple concurrent users. However, the moment you deploy to a host with more than one person accessing your app, you'll find that you're sharing a session and seeing other people's updates. That isn't what most developers have in mind.
You can use your default Rails encrypted cookie-based sessions to isolate your users into their own sessions. This works great even if your application doesn't have a login system.
app/controllers/application_controller.rbclass ApplicationController < ActionController::Basebefore_action :set_action_cable_identifier​private​def set_action_cable_identifiercookies.encrypted[:session_id] = session.id.to_sendend
app/channels/application_cable/connection.rbmodule ApplicationCableclass Connection < ActionCable::Connection::Baseidentified_by :session_id​def connectself.session_id = cookies.encrypted[:session_id]endendend
We need to instruct ActionCable to stream updates on a per-session basis:
app/channels/optimism_channel.rbclass OptimismChannel < ApplicationCable::Channeldef subscribedstream_for session_idendend
Finally, we need to give Optimism the ability to broadcast updates to the correct channel subscription. We will override Optimism's default "OptimismChannel" with a lambda that returns the subscription identifier. You might already have other values in your initializer, or it might not yet exist at all:
config/initializers/optimism.rbOptimism.configure do |config|config.channel_proc = ->(context) { OptimismChannel.broadcasting_for(context.session.id) }end
Many Rails apps use the current_user convention or more recently, the Current object to provide a global user context. This gives access to the user scope from almost all parts of your application.
app/controllers/application_controller.rbclass ApplicationController < ActionController::Basebefore_action :set_action_cable_identifier​private​def set_action_cable_identifiercookies.encrypted[:user_id] = current_user&.idendend
app/channels/application_cable/connection.rbmodule ApplicationCableclass Connection < ActionCable::Connection::Baseidentified_by :current_user​def connectuser_id = cookies.encrypted[:user_id]return reject_unauthorized_connection if user_id.nil?user = User.find_by(id: user_id)return reject_unauthorized_connection if user.nil?self.current_user = userendendend
We need to instruct ActionCable to stream updates on a per-session basis:
app/channels/optimism_channel.rbclass OptimismChannel < ApplicationCable::Channeldef subscribedstream_for current_userendend
Finally, we need to give Optimism the ability to broadcast updates to the correct channel subscription. We will override Optimism's default "OptimismChannel" with a lambda that returns the subscription identifier. You might already have other values in your initializer, or it might not yet exist at all:
config/initializers/optimism.rbOptimism.configure do |config|config.channel_proc = ->(context) { OptimismChannel.broadcasting_for(context.current_user) }end
If you're using the versatile Devise authentication library, your configuration is identical to the User-Based Authentication above, except for how ActionCable looks up a user:
app/channels/application_cable/connection.rbmodule ApplicationCableclass Connection < ActionCable::Connection::Baseidentified_by :current_user​def connectself.current_user = find_verified_userend​protected​def find_verified_userif current_user = env["warden"].usercurrent_userelsereject_unauthorized_connectionendendendend