# app/channels/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
def subscribed
# Subscribe to a specific room
room = Room.find(params[:room_id])
# Authorize user
reject unless current_user.can_access?(room)
stream_for room
# Track user presence
room.users << current_user unless room.users.include?(current_user)
broadcast_user_joined(room)
end
def unsubscribed
room = Room.find(params[:room_id])
room.users.delete(current_user)
broadcast_user_left(room)
end
def receive(data)
# Receive message from client
room = Room.find(params[:room_id])
message = room.messages.create!(
user: current_user,
content: data['message']
)
# Broadcast to all subscribed clients
ChatChannel.broadcast_to(room, {
type: 'message',
message: MessageSerializer.new(message).as_json,
user: UserSerializer.new(current_user).as_json
})
end
def typing(data)
room = Room.find(params[:room_id])
ChatChannel.broadcast_to(room, {
type: 'typing',
user_id: current_user.id,
user_name: current_user.name
})
end
private
def broadcast_user_joined(room)
ChatChannel.broadcast_to(room, {
type: 'user_joined',
user: UserSerializer.new(current_user).as_json,
users_count: room.users.count
})
end
def broadcast_user_left(room)
ChatChannel.broadcast_to(room, {
type: 'user_left',
user_id: current_user.id,
users_count: room.users.count
})
end
end
# app/channels/application_cable/connection.rb
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
if verified_user = User.find_by(id: cookies.encrypted[:user_id])
verified_user
else
reject_unauthorized_connection
end
end
end
end
# app/channels/notifications_channel.rb
class NotificationsChannel < ApplicationCable::Channel
def subscribed
stream_for current_user
end
def unsubscribed
stop_all_streams
end
def mark_as_read(data)
notification = current_user.notifications.find(data['id'])
notification.mark_as_read!
end
end
# Broadcasting from model
class Notification < ApplicationRecord
belongs_to :user
after_create_commit do
broadcast_notification
end
private
def broadcast_notification
NotificationsChannel.broadcast_to(
user,
{
type: 'notification',
notification: NotificationSerializer.new(self).as_json
}
)
end
end
# Broadcasting from anywhere
class CommentService
def create_comment(post, user, content)
comment = post.comments.create!(user: user, content: content)
# Notify post author
notification = post.user.notifications.create!(
message: "#{user.name} commented on your post",
link: post_path(post)
)
# Real-time notification
NotificationsChannel.broadcast_to(
post.user,
NotificationSerializer.new(notification).as_json
)
comment
end
end
# Streaming from a model
class ActivityChannel < ApplicationCable::Channel
def subscribed
stream_from "activity:#{current_user.id}"
end
end
# Broadcasting to stream
ActionCable.server.broadcast(
"activity:#{user.id}",
{ type: 'new_follower', follower: follower.as_json }
)
# Periodic broadcasts (app/jobs/server_stats_job.rb)
class ServerStatsJob < ApplicationJob
def perform
stats = {
users_online: User.online.count,
cpu_usage: SystemStats.cpu_usage,
memory_usage: SystemStats.memory_usage
}
ActionCable.server.broadcast('server_stats', stats)
end
end
// app/javascript/channels/chat_channel.js
import consumer from "./consumer"
const chatChannel = consumer.subscriptions.create(
{
channel: "ChatChannel",
room_id: document.getElementById('room-id').value
},
{
connected() {
console.log("Connected to chat channel")
},
disconnected() {
console.log("Disconnected from chat")
},
received(data) {
switch(data.type) {
case 'message':
this.appendMessage(data.message)
break
case 'typing':
this.showTypingIndicator(data.user_name)
break
case 'user_joined':
this.updateUsersCount(data.users_count)
break
}
},
send_message(message) {
this.perform('receive', { message: message })
},
typing() {
this.perform('typing')
},
appendMessage(message) {
const messagesContainer = document.getElementById('messages')
messagesContainer.insertAdjacentHTML('beforeend', message.html)
},
showTypingIndicator(userName) {
// Show "{userName} is typing..."
}
}
)
// Send message on form submit
document.getElementById('message-form').addEventListener('submit', (e) => {
e.preventDefault()
const input = e.target.querySelector('input')
chatChannel.send_message(input.value)
input.value = ''
})
ActionCable integrates WebSockets seamlessly with Rails. Channels handle pub/sub messaging between server and clients. I use ActionCable for chat, notifications, live updates. Channels subscribe clients to streams; broadcasting pushes data to subscribed clients. Connection authorization ensures only authenticated users connect. Channel callbacks—subscribed, unsubscribed, receive—handle lifecycle events. Streaming from models broadcasts ActiveRecord changes automatically. ActionCable scales with Redis for multi-server deployments. Testing channels uses connectionstub and subscriptionstub. Understanding ActionCable's relationship with Turbo Streams unlocks powerful real-time Rails UIs. ActionCable brings WebSocket simplicity to Rails without external services.
Related snips
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency = 'USD')
Value objects for domain modeling
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
Share this code
Here's the card — post it anywhere.