Rails 6.1 has added a new method
It’s a convenience method, to start the stream or reject the connection, based on whether record is present or blank.
To use the method, in our channel class, we can do as follows:
class CommentaryChannel < ApplicationCable::Channel def subscribed record = Match.find_by(id: params[:match_id]) stream_or_reject_for record end end
This method on
checks if the
record is present and
if it is,
then starts the stream for
record is not present it rejects the connection.
stream_or_reject_for provides a common abstraction
in ActionCable to stream when data is available
else reject the connect.