Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel#broadcast doesn't work like broadcast_to #93

Open
jwoertink opened this issue Nov 6, 2024 · 0 comments
Open

Channel#broadcast doesn't work like broadcast_to #93

jwoertink opened this issue Nov 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@jwoertink
Copy link
Collaborator

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.

@jwoertink jwoertink added the bug Something isn't working label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant