如何将 ActionCable broadcast_to 用于特定用户组?
How can use ActionCable broadcast_to with specific group of users?
我有一个基本设置 rails API + devise_token_auth + 用于前端的可操作和 angular9 应用程序。
我基本上想向特定用户组而不是所有用户发送通知。
我的代码如下所示:
connections.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
uid = request.query_parameters[:uid]
token = request.query_parameters[:token]
client_id = request.query_parameters[:client]
user = User.find_by_uid(uid)
if user && user.valid_token?(token, client_id)
user
else
reject_unauthorized_connection
end
end
end
end
appointments.rb
class Appointment < ApplicationRecord
validates_uniqueness_of :start_time
belongs_to :slug
belongs_to :user
belongs_to :property
belongs_to :contact
after_create do
ChangeAppointmentsJob.perform_later self
end
after_update do
ChangeAppointmentsJob.perform_later self
end
end
ChangeAppointmentsJob
class ChangeAppointmentsJob < ApplicationJob
queue_as :default
def perform(appointment)
ActionCable.server.broadcast 'appointment_channel', data: appointment
end
end
约会频道
class AppointmentChannel < ApplicationCable::Channel
def subscribed
stream_from 'appointment_channel'
end
def unsubscribed
stop_all_streams
end
end
我想做这样的事情
ActionCable.server.broadcast_to( User.where(slug_id: current_user.slug_id) )
向我的 Front 应用程序发送通知,但 不是所有用户,只是 slug_id 喜欢创建或更新约会的用户
任何想法,请!
我找到了解决方案
我发送 slug_id 和 uid,access_token 和订阅频道的客户端,我在 connection.rb 中像这样使用它
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
uid = request.query_parameters[:uid]
token = request.query_parameters[:token]
client_id = request.query_parameters[:client]
slug_id = request.query_parameters[:slug_id]
user = User.where(slug_id: slug_id).find_by_uid(uid) <-------- get all users with the same slug_id :)
if user && user.valid_token?(token, client_id)
user
else
reject_unauthorized_connection
end
end
end
end
我有一个基本设置 rails API + devise_token_auth + 用于前端的可操作和 angular9 应用程序。
我基本上想向特定用户组而不是所有用户发送通知。
我的代码如下所示:
connections.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
uid = request.query_parameters[:uid]
token = request.query_parameters[:token]
client_id = request.query_parameters[:client]
user = User.find_by_uid(uid)
if user && user.valid_token?(token, client_id)
user
else
reject_unauthorized_connection
end
end
end
end
appointments.rb
class Appointment < ApplicationRecord
validates_uniqueness_of :start_time
belongs_to :slug
belongs_to :user
belongs_to :property
belongs_to :contact
after_create do
ChangeAppointmentsJob.perform_later self
end
after_update do
ChangeAppointmentsJob.perform_later self
end
end
ChangeAppointmentsJob
class ChangeAppointmentsJob < ApplicationJob
queue_as :default
def perform(appointment)
ActionCable.server.broadcast 'appointment_channel', data: appointment
end
end
约会频道
class AppointmentChannel < ApplicationCable::Channel
def subscribed
stream_from 'appointment_channel'
end
def unsubscribed
stop_all_streams
end
end
我想做这样的事情
ActionCable.server.broadcast_to( User.where(slug_id: current_user.slug_id) )
向我的 Front 应用程序发送通知,但 不是所有用户,只是 slug_id 喜欢创建或更新约会的用户
任何想法,请!
我找到了解决方案
我发送 slug_id 和 uid,access_token 和订阅频道的客户端,我在 connection.rb 中像这样使用它
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
uid = request.query_parameters[:uid]
token = request.query_parameters[:token]
client_id = request.query_parameters[:client]
slug_id = request.query_parameters[:slug_id]
user = User.where(slug_id: slug_id).find_by_uid(uid) <-------- get all users with the same slug_id :)
if user && user.valid_token?(token, client_id)
user
else
reject_unauthorized_connection
end
end
end
end