Skip to content

Commit

Permalink
fixup! Allow the application to configure Turbo::StreamChannel’s inhe…
Browse files Browse the repository at this point in the history
…ritance
  • Loading branch information
Nicklas Ramhöj Holtryd committed Oct 22, 2024
1 parent 6576ec8 commit e674f71
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions app/channels/turbo/streams_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,21 @@
# using the view helper <tt>Turbo::StreamsHelper#turbo_stream_from(*streamables)</tt>.
# 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 <tt>Turbo::Streams::StreamName</tt>:
# It's important to understand that while stream names are signed, <tt>Turbo::StreamsChannel</tt> doesn't authenticate connections or
# authorize subscriptions. You can configure <tt>Turbo::StreamChannel</tt> to use e.g your <tt>ApplicationCable::Channel</tt> to
# implement authorization:
#
# class CustomChannel < ActionCable::Channel::Base
# extend Turbo::Streams::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
# # config/initializers/turbo.rb
# Rails.application.config.to_prepare do
# Turbo.base_stream_channel_class = "ApplicationCable::Channel"
# 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 %>
# You can also choose which channel to use via:
# <%= turbo_stream_from "room", channel: CustomChannel %>
#
# Note that any channel that listens to a <tt>Turbo::Broadcastable</tt> compatible stream name
# (e.g <tt>verified_stream_name_from_params</tt>) can also be subscribed to via <tt>Turbo::StreamsChannel</tt>. Meaning that you should
# never use the <tt>turbo_stream_from</tt> <tt>:channel</tt> option to implement authorization.
class Turbo::StreamsChannel < Turbo.base_stream_channel_class.constantize
extend Turbo::Streams::Broadcasts, Turbo::Streams::StreamName
include Turbo::Streams::StreamName::ClassMethods
Expand Down

0 comments on commit e674f71

Please sign in to comment.