Skip to content

Channel#broadcast doesn't work like broadcast_to #93

@jwoertink

Description

@jwoertink

Channel has a broadcast and self.broadcast_to method. In production, I've been using the class method for sending chat messages.

class ChatChannel < ApplicationCable::Channel
  def subscribed
    stream_from("chat:room_1") 
  end
  def perform(action, data)
    ChatChannel.broadcast_to("chat:room_1", "some message")
  end
end

This works great. Then while doing some code cleanup I was questioning why I wasn't using the instance method to broadcast in here... So I change it to this

class ChatChannel < ApplicationCable::Channel
  def subscribed
    stream_from("chat:room_1") 
  end
  def perform(action, data)
    broadcast("some message")
  end
end

In my mind, this method was cleaner. I tested locally and in staging and it worked fine. No issues.. So I deploy to production, and my chat wasn't necessarily completely broken, but it also wasn't working great. When sending a chat message, only some people would get it. Not everyone. Then when everyone would do a hard refresh, other people would get the messages and new people wouldn't. It was almost like it could only broadcast to like 5 people, but with 10 in chat, half the people wouldn't get it....

It's a very confusing issue. Reverting back to using the broadcast_to fixes it all for me, so I have no clue but wanted to at least document it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions