Ruby on rails 使用React前端中的Rails ActionCable对象。
我有一个Ruby on rails 使用React前端中的Rails ActionCable对象。,ruby-on-rails,websocket,actioncable,Ruby On Rails,Websocket,Actioncable,我有一个约会模型,每次创建或更新约会时,我都希望在react中向前端应用程序发送广播。这是我的模型的代码 class Appointment < ApplicationRecord belongs_to :tutor, class_name: 'User' belongs_to :student, class_name: 'User' after_create :appointment_notification after_update :appointment_not
约会
模型,每次创建或更新约会时,我都希望在react中向前端应用程序发送广播。这是我的模型的代码
class Appointment < ApplicationRecord
belongs_to :tutor, class_name: 'User'
belongs_to :student, class_name: 'User'
after_create :appointment_notification
after_update :appointment_notification
def appointment_notification
Notification.create(
from: student,
to: tutor,
name: :student_create_appointment, # here you can detect any type
model: :appointment
)
end
end
我创建了一个通知annel
,看起来是这样的:
class NotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from "notification_channel:#{current_user.id}"
end
def unsubscribed
stop_all_streams
end
end
一切都很好,唯一缺少的链接是我在前端向用户广播的部分:
以下是我到目前为止在JavaScript方面的内容
App.notificationsChannel = App.cable.subscriptions.create(
'NotificationsChannel',
{
connected: function() {
// Called when the subscription is ready for use on the server
console.log('Notification Channel connected.');
},
disconnected: function() {
// Called when the subscription has been terminated by the server
console.log('Notification Channel disconnected.');
},
received: function(data) {
// Called when there's incoming data on the websocket for this channel
console.log(data);
}
}
);
// App.notificationsChannel.send({ test: 'data' });
除了连接和断开连接中的内容外,我无法在浏览器控制台中打印任何内容
创建约会后,这就是我的终端日志
你知道我还缺少什么,我需要做什么吗
顺便说一句,我还在路由文件中创建了这些路由URL
resources :notifications, only: :index do
collection do
post 'seen_all', to: "notifications#seen_all"
end
member do
post :seen
end
end
最后是我的通知控制器
module API
module V1
class NotificationsController < ApiController
before_action :set_user
def index
@user.incoming_notifications.page(params[:page]).per(params[:per_page])
end
def seen_all
Notification.where(seen: false, to_id: @user.id).update(seen: true)
end
def seen
@user.incoming_notifications.find_by(id: params[:id]).seen!
end
private
def set_user
@user = current_user
end
end
end
end
模块API
模块V1
类通知控制器
请指导我如何在浏览器控制台中打印通知,然后通过API在React中使用通知。谢谢 您可以使用以下教程来实现此功能。
这很容易实现
我已经遵循了一些项目的指导原则。您可以使用以下教程来实现这一点。
这很容易实现
我已经遵循了一些项目的指导原则。有关更多信息,请查看延迟的作业日志。
尝试以下方法:
def perform(from:, to:, message:, name:, created_at:)
ActionCable.server.broadcast "notification_channel:#{to.id}", { type: type, caller: caller.name, message: message, created_at: created_at }
end
这里有一个提示:在提交后使用:在:[:创建,:更新]
上做某事,而不是在创建后使用。这将确保只有在成功创建约会后才会触发通知。有关详细信息,请查看延迟的作业日志。
尝试以下方法:
def perform(from:, to:, message:, name:, created_at:)
ActionCable.server.broadcast "notification_channel:#{to.id}", { type: type, caller: caller.name, message: message, created_at: created_at }
end
这里有一个提示:在提交后使用:在:[:创建,:更新]
上做某事,而不是在创建后使用。这将确保只有在成功创建约会后才会触发通知
def perform(from:, to:, message:, name:, created_at:)
ActionCable.server.broadcast "notification_channel:#{to.id}", { type: type, caller: caller.name, message: message, created_at: created_at }
end