私はアクションケーブルと苦労しています。私の場合、私は(Devise
経由で)お互いにタスクを共有できる2人のユーザーがいます。レールアクションケーブル特定の消費者
が(フォーム経由で)user#2
でタスクを共有すると、すべての認証済みユーザーが通知を受け取ります。
user#2
はどのようにして自分にブロードキャストする必要がありますか?ここで
connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
logger.add_tags 'ActionCable', current_user.id
end
protected
def find_verified_user # this checks whether a user is authenticated with devise
if verified_user = env['warden'].user
verified_user
else
reject_unauthorized_connection
end
end
end
end
cable.js
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
todo_channel.rb
class TodoChannel < ApplicationCable::Channel
def subscribed
stream_from "todo_channel_#{current_user.id}"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def notify
ActionCable.server.broadcast "todo_channel_#{current_user.id}", message: 'some message'(not implemented yet)
end
end
私はあなたが実際にチャンネルにstream_from
を複数回呼び出すことができますし、そのユーザが複数の異なる「部屋」に加入されることを実現するまで私が前に似た何かを直面してきました
App.todo = App.cable.subscriptions.create "TodoChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
console.log(data['message'])
notify: ->
@perform 'notify'
この 'todo_channel.rb'コードは、' app/channels/application_cable/connection.rb'が 'current_user'を提供するように設定されている場合にのみ機能します。実際、これにはセキュリティを含む例があります(https://github.com/rails/rails/tree/master/actioncable#examples) – brod