Skip to content

Instantly share code, notes, and snippets.

@dhh
Last active December 16, 2020 14:24
Show Gist options
  • Save dhh/64b21e7148c4926a4228 to your computer and use it in GitHub Desktop.
Save dhh/64b21e7148c4926a4228 to your computer and use it in GitHub Desktop.
On-boarding a specialized broadcast method in the channel itself
# 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