I created the following using ActionCable but not able to receive any data that is being broadcasted.
Comments Channel:
class CommentsChannel < ApplicationCable::Channel
def subscribed
comment = Comment.find(params[:id])
stream_for comment
end
end
JavaScript:
var cable = Cable.createConsumer('ws://localhost:3000/cable');
var subscription = cable.subscriptions.create({
channel: "CommentsChannel",
id: 1
},{
received: function(data) {
console.log("Received data")
}
});
It connects fine and I can see the following in the logs:
CommentsChannel is streaming from comments:Z2lkOi8vdHJhZGUtc2hvdy9FdmVudC8x
I then broadcast to that stream:
ActionCable.server.broadcast "comments:Z2lkOi8vdHJhZGUtc2hvdy9FdmVudC8x", { test: '123' }
The issue is that the received
function is never called. Am I doing something wrong?
Note: I'm using the actioncable
npm package to connect from a BackboneJS application.
Changing the cable adapter from async
to redis
in config/cable.yml
fixed it for me.
Update
As Erem pointed out below, the server and console are isolated processes so you need to use a centralized queue manager.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With