ActionCable Not Receiving Data

梦想的初衷 提交于 2020-01-01 01:18:07

问题


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.


回答1:


Changing the cable adapter from async to redis in config/cable.yml fixed it for me. Not sure why async was not working.



来源:https://stackoverflow.com/questions/36266746/actioncable-not-receiving-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!