Last active
December 16, 2020 14:24
-
-
Save dhh/64b21e7148c4926a4228 to your computer and use it in GitHub Desktop.
On-boarding a specialized broadcast method in the channel itself
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Channel | |
class CommentsChannel < ApplicationCable::Channel | |
def self.broadcast_comment(comment) | |
broadcast_to comment.message, comment: CommentsController.render( | |
partial: 'comments/comment', locals: { comment: comment } | |
) | |
end | |
def follow(data) | |
stop_all_streams | |
stream_for current_user.account.messages.find(data['message_id']) | |
rescue ActiveRecord::RecordNotFound | |
reject | |
end | |
def unfollow | |
stop_all_streams | |
end | |
end | |
# Comment | |
class Comment < ApplicationRecord | |
belongs_to :message | |
after_create_commit -> { CommentsChannel.broadcast_comment(self) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment