ActionCable Not Broadcasting Messages to All Clients in Rails 7.0.4.3

Hello Rails Community! This is my first message here. I’m stuck for some days already trying to solve this issue below.

I am developing a chat application using ActionCable in Rails 7.0.4.3, but I am encountering a problem where messages aren’t being broadcast to all connected clients. I followed a tutorial for setting up the ActionCable connection, including setting up a ChatChannel and subscribing to it in the client-side JavaScript. Here’s the setup I have:

chat_channel.rb:

rubyCopy code

class ChatChannel < ApplicationCable::Channel
  def subscribed
    stream_from "chat"
  end

  def speak(data)
    ActionCable.server.broadcast('chat', message: data['message'])
  end
end

chat_channel.js:

javascriptCopy code

import { createConsumer } from "@rails/actioncable"

const consumer = createConsumer()

const chatChannel = consumer.subscriptions.create("ChatChannel", {
  connected() {
    console.log("Connected to the chat!");
  },

  disconnected() {
    console.log("Disconnected from the chat.");
  },

  received(data) {
    console.log("Received data: ", data);
  },

  speak(message) {
    this.perform('speak', { message: message });
  }
});

window.chatChannel = chatChannel;

I’m testing this by opening two different browsers and connecting to the chat. When I send a message from one client using chatChannel.speak('Hello, World!'), I see the server log the broadcast message, but the other client does not receive the message. In fact, the received function does not seem to be triggered at all.

Here’s the server log when I send a message:

ChatChannel#speak({"message"=>"Hello, World!"})
[ActionCable] Broadcasting to chat: "Hello, World!"
ChatChannel is transmitting the subscription confirmation
ChatChannel is streaming from chat_channel

I’ve checked that the ActionCable consumer is set up correctly in my JavaScript, and I don’t see any errors in the console. Can anyone provide any insight into what might be going wrong here?

Turbo is disabled on my application, but I don’t think it’s influencing here.

Any help would be greatly appreciated. Thanks in advance!

Any direction to solve this? Still had no success.

Hi! Looking at your logs, it seems really strange that you’re getting a subscription confirmation after the speak method is called. Is there a chance that your second window/tab is reloading or something from some other script? Very strange.

Secondly, one tip to make troubleshooting a lot easier I’ve found is that you can pop open your browser dev tools, head to the Network tab, select WS as the filter (for web sockets) and then click on the /cable entry. You can then see all the messages sent between your browser and the server and it should help you diagnose where a message is getting dropped.

Hang in there! Once you get it working it really is very smooth. ActionCable always seems to have a few hiccups the first few times you use it.

Hello,

I am too experiencing this issue in a different way. Redis is working and can see its getting messages. From console, when I send a broadcast message, it pops on the index page. However when I try to broadcast it from controller action, I see messages are received only when page is refreshed(I can see in received(data) console logs, but they disappear. I am using the same broadcast method in the controller to send broadcast as below.

class HelloController < ApplicationController
  def index
    ActionCable.server.broadcast("chat_channel", { title: "Hello From Rails" })
    render "index"
  end
end

This is really strange as messages appear momentarily(only during the time the page refreshes and then disappear). Looks like, broadcast message does not stay persistent. I have spent whole 2 days with it and really can’t understand what’s going on?

Hi @Chandan1980 - Since the received method is getting called, are you doing anything client-side with js that would be causing them to disappear? I’m assuming you mean that they disappear from the DOM? Any chance you could provide a little more info?

Hi @godwincodes , This is fixed now. I used the latest version of Rails 7.0.6 which uses Hotwire Turbo by default.

Hi @Chandan1980 , may I ask how did you fix this? It seems that I’m encountering the same problem. Thanks .