or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

connection-management.mdindex.mdjavascript-client.mdrails-integration.mdserver-broadcasting.mdsubscription-adapters.md
tile.json

index.mddocs/

ActionCable

ActionCable 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.

Package Information

  • Package Name: actioncable
  • Package Type: gem
  • Language: Ruby with JavaScript client components
  • Installation: gem install actioncable or add to Gemfile: gem 'actioncable'

Core Imports

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);
  }
});

Basic Usage

Server-side Channel Implementation

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

Client-side Usage

// 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!' });

Architecture

ActionCable is built around several key components:

  • Server Singleton: ActionCable.server provides the main server instance for broadcasting and connection management
  • Channels: Ruby classes that handle WebSocket subscriptions and define actions clients can call
  • Connections: Handle WebSocket authentication, authorization, and routing to appropriate channels
  • Broadcasting System: Pub/sub mechanism for sending messages to channel subscribers across multiple server processes
  • Subscription Adapters: Pluggable backends (Redis, PostgreSQL, in-memory) for scaling across multiple servers
  • JavaScript Client: Browser-side consumer that manages WebSocket connections and channel subscriptions

Capabilities

Server Broadcasting

Core 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
end

Server Broadcasting

Connection Management

WebSocket 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
end

Connection Management

Subscription Adapters

Pluggable 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
end

Subscription Adapters

JavaScript Client

Complete 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
}

JavaScript Client

Rails Integration

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
end

Rails Integration

Core Types

module 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