Reputation: 11
I am trying to migrate a rails application from using Pusher to ActionCable. In the server side code this is the ActionCable Channel:
class PagesChannel < ApplicationCable::Channel
def subscribed
stream_from 'pages'
end
def speak(data)
ActionCable.server.broadcast('pages',data)
end
end
and on the client side this is the subscriber code:
const app=ActionCable.createConsumer("wss://localhost.branch.co:3000/cable");
const channel =app.subscriptions.create("PagesChannel",{
connected:()=>{
console.log("connected")
},
received:()=>{
console.log("got mail")
}
})
In the official website https://guides.rubyonrails.org/action_cable_overview.html It's mentioned that:
Then, elsewhere in your Rails application, you can broadcast to such a room by calling broadcast:
I interpreted that to mean that in my rails app, I can call
ActionCable.server.broadcast('pages',data)
and It will be broadcasted to all subscribers.
Can you call this only inside a Channel class? and if so how do you call a method of a channel class from server side.
I am able to verify that my channel subscriptions from client are connected. I can even call
channel.perform("speak");
in my JS code from my client and have it broadcast to all clients.
I am running a server above code , all configs, routes.rb paths are all set. After running the server,
I am using rails console via
bundle exec rails console
and typing
ActionCable.server.broadcast("pages",{})
there is no received message on client side, no errors are shown in console.
my client is running above code.
I have made sure it isn't a cross domain error.
Upvotes: 1
Views: 534
Reputation: 11
It is hard to help without seeing the whole app, but you should be able to call ActionCable.server.broadcast('pages',data)
from anywhere.
My guess is, you don't have redis configured correctly, you should verify that.
Upvotes: 1