What is Rails' ActionCable?

Action cable allows real-time features in ruby using Websockets using the Publish Subscribe pattern

Server Side
Services maintain connections to client /consumers,/ there is one consumer to connection per browser (tab). Usually a connection is maintained and carried with the save cookie used to log in.

A channel is a logical unit of work (controller in MVC)

A consumer subscribes to a channel as a subscription, messages are routed to this channel

Client Side
Consumers need a connection instance on their side via JavaScript.

An example of a subscription


App.cable.subscriptions.create { channel: "AppearanceChannel"} 

Interactions between Server-Client

A stream is way for channels to route content (as broadcasts) to subscribers

# app/channels/chat_channel.rb

class ChatChannel < ApplicationCable::Channel
  def subscribed 
    stream_from "chat_#{params[:room]}"

A broadcast is a pub/sub link, allowing you to directly publish to a channel of subscribers.

  title: 'New things!',
  body: 'All the news fit to print'