WebSocket framework for Rails that structures real-time application concerns into channels over a single WebSocket connection.
pkg:gem/actioncable@5.2.x
npx @tessl/cli install tessl/gem-actioncable@5.2.0ActionCable is a full-stack WebSocket framework that seamlessly integrates real-time features into Rails applications. It provides both server-side Ruby framework components for handling WebSocket connections, channels, and broadcasting, as well as client-side JavaScript components for establishing and managing WebSocket connections from browsers. The library enables developers to build real-time features like chat systems, live updates, and collaborative applications using familiar Rails patterns and conventions.
gem install actioncable or add to Gemfile: gem 'actioncable'Ruby server-side:
require 'action_cable'
# Access the singleton server instance
server = ActionCable.server
# Broadcasting to channels
ActionCable.server.broadcast("chat_#{room_id}", { message: "Hello" })JavaScript client-side:
// Rails asset pipeline
//= require action_cable
// Create consumer and connect
App.cable = ActionCable.createConsumer();
// Create subscription
App.chatChannel = App.cable.subscriptions.create("ChatChannel", {
received: function(data) {
console.log(data);
}
});class ChatChannel < ApplicationCable::Channel
def subscribed
stream_from "chat_#{params[:room_id]}"
end
def unsubscribed
# Cleanup when subscription is destroyed
end
def speak(data)
# Broadcast to all subscribers of this room
ActionCable.server.broadcast("chat_#{params[:room_id]}", {
message: data['message'],
user: current_user.name
})
end
end// Create consumer connection
const consumer = ActionCable.createConsumer('/cable');
// Subscribe to a channel
const subscription = consumer.subscriptions.create(
{ channel: "ChatChannel", room_id: 1 },
{
connected() {
console.log("Connected to chat");
},
disconnected() {
console.log("Disconnected from chat");
},
received(data) {
console.log("Received:", data);
// Handle incoming messages
}
}
);
// Send data to channel
subscription.perform('speak', { message: 'Hello everyone!' });ActionCable is built around several key components:
ActionCable.server provides the main server instance for broadcasting and connection managementCore broadcasting functionality for sending messages to channel subscribers. Supports both direct broadcasting and broadcaster instances for reuse.
module ActionCable
def self.server
@server ||= ActionCable::Server::Base.new
end
end
class ActionCable::Server::Base
def broadcast(broadcasting, message, coder: ActiveSupport::JSON)
end
def broadcaster_for(broadcasting, coder: ActiveSupport::JSON)
end
endWebSocket connection handling including authentication, authorization, identification, and remote connection management across servers.
class ActionCable::Connection::Base
def self.identified_by(*identifiers)
end
def reject_unauthorized_connection
end
end
class ActionCable::RemoteConnections
def where(identifier)
end
endPluggable pub/sub backends for scaling ActionCable across multiple server processes and machines.
class ActionCable::SubscriptionAdapter::Base
def broadcast(channel, payload)
end
def subscribe(channel, message_callback, success_callback = nil)
end
def unsubscribe(channel, message_callback)
end
endComplete client-side API for establishing WebSocket connections, managing subscriptions, and handling real-time communication.
ActionCable.createConsumer(url?: string): Consumer
class Consumer {
connect(): void
disconnect(): void
subscriptions: Subscriptions
}
class Subscriptions {
create(channelName: string | object, mixin?: object): Subscription
}Rails engine integration including helpers for generating meta tags, generators for creating channels, and configuration management.
module ActionCable::Helpers::ActionCableHelper
def action_cable_meta_tag
end
end
class Rails::Generators::ChannelGenerator < NamedBase
def create_channel_file
end
endmodule ActionCable
INTERNAL = {
message_types: {
welcome: "welcome",
ping: "ping",
confirmation: "confirm_subscription",
rejection: "reject_subscription"
},
default_mount_path: "/cable",
protocols: ["actioncable-v1-json", "actioncable-unsupported"]
}
end