【发布时间】:2018-11-27 03:24:09
【问题描述】:
我的应用程序中有 2 种用户类型(员工和公司)。这两种用户类型都是用 Devise 创建的。我目前正在尝试使用 ActionCable 向特定公司发送通知。
我的主要问题是,当我发送通知时,每个登录的公司都会收到通知。我知道我应该以某种方式在流名称中包含公司 ID,但到目前为止我还没有运气。
我在下面包含了向所有公司发送通知的工作代码:
notifications_channel.rb
class NotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from "notifications_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
end
end
呼叫广播
ActionCable.server.broadcast 'notifications_channel', { 'My data' }
编辑
我用 javascript 记录通知的状态:
notifications.js
App.notifications = App.cable.subscriptions.create("NotificationsChannel", {
connected: function() {
console.log("connected");
};
disconnected: function() {
console.log("disconnected");
};
received: function(data) {
console.log("recieved");
};
});
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-5 actioncable