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.
Related snips
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
ActiveRecord scopes for reusable query logic
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
class PostsController < ApplicationController
def index
@posts = Post.includes(:author)
.order(created_at: :desc)
.page(params[:page])
.per(10)
Turbo Frames: infinite scroll with lazy-loading frame
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
<form data-controller="query-sync" data-action="change->query-sync#apply">
<select name="status" class="rounded border p-2">
<option value="">Any</option>
<option value="open">Open</option>
<option value="closed">Closed</option>
</select>
Filter UI that syncs query params via Stimulus (no front-end router)
Share this code
Here's the card — post it anywhere.