Rails 6.1 adds ActionCable::Channel#stream_or_reject_for

Rails 6.1 has added a new method ActionCable::Channel#stream_or_reject_for.

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 ActionCable::Channel checks if the record is present and if it is, then starts the stream for record object. If record is not present it rejects the connection.

Summary

stream_or_reject_for provides a common abstraction in ActionCable to stream when data is available else reject the connect.

Need help on your Ruby on Rails or React project?

Join Our Newsletter