class NotificationsChannel < ApplicationCable::Channel
def subscribed
stream_for current_user
end
def unsubscribed
stop_all_streams
end
end
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
token = request.params[:token]
payload = JwtService.decode(token)
if payload && (user = User.find_by(id: payload['user_id']))
user
else
reject_unauthorized_connection
end
end
end
end
class Notification < ApplicationRecord
belongs_to :user
after_create_commit :broadcast_notification
private
def broadcast_notification
NotificationsChannel.broadcast_to(
user,
{
id: id,
message: message,
created_at: created_at,
read: false
}
)
end
end
Action Cable brings WebSocket support to Rails, enabling real-time features like live notifications, collaborative editing, or chat systems. Clients subscribe to channels that broadcast updates when server-side events occur. I use Redis as the pub/sub backend so messages propagate across multiple application servers. Each channel class defines subscribed and unsubscribed lifecycle hooks, and broadcast happens via ActionCable.server.broadcast. Authentication happens during the connection handshake using the same session-based or token-based approach as HTTP requests. The key challenge is scaling—WebSockets hold persistent connections, so I often isolate Action Cable servers from HTTP servers to optimize resource allocation independently.