Class: Turbo::StreamsChannel

Inherits:
ActionCable::Channel::Base
  • Object
show all
Extended by:
Turbo::Streams::Broadcasts, Turbo::Streams::StreamName
Includes:
Turbo::Streams::StreamName::ClassMethods
Defined in:
app/channels/turbo/streams_channel.rb

Overview

The streams channel delivers all the turbo-stream actions created (primarily) through Turbo::Broadcastable. A subscription to this channel is made for each individual stream that one wishes to listen for updates to. The subscription relies on being passed a signed_stream_name parameter generated by turning a set of streamables into signed stream name using Turbo::Streams::StreamName#signed_stream_name. This is automatically done using the view helper Turbo::StreamsHelper#turbo_stream_from(*streamables). If the signed stream name cannot be verified, the subscription is rejected.

In case if custom behavior is desired, one can create their own channel and re-use some of the primitives from helper modules like Turbo::Streams::StreamName:

 class CustomChannel < ActionCable::Channel::Base
    extend Turbo::Stream::Broadcasts, Turbo::Streams::StreamName
    include Turbo::Streams::StreamName::ClassMethods

    def subscribed
      if (stream_name = verified_stream_name_from_params).present? &&
         subscription_allowed?
        stream_from stream_name
      else
        reject
      end
    end

    def subscription_allowed?
       # ...
    end
 end

This channel can be connected to a web page using <tt>:channel</tt> option in
<tt>turbo_stream_from</tt> helper:

  <%= turbo_stream_from 'room', channel: CustomChannel %>

Instance Method Summary collapse

Instance Method Details

#subscribedObject



38
39
40
41
42
43
44
# File 'app/channels/turbo/streams_channel.rb', line 38

def subscribed
  if stream_name = verified_stream_name_from_params
    stream_from stream_name
  else
    reject
  end
end