我已设置 ActionCable 并使用通用消息通道。但是,我需要将频道订阅限制为单个消息。我应该如何将 message_id 传递给订阅?
我有一个包含正在查看/订阅的 message_id 的数据属性。
评论频道.js
import consumer from "./consumer"
var get_id = $('#messages').data('message-id')
consumer.subscriptions.create({
channel: "CommentChannel", message_id: get_id
}, {
connected() {
console.log('connected to the comment channel')
},
disconnected() {
console.log('disconnected to the comment channel')
},
received(data) {
console.log(data);
$('section#comments').append(data['comment']);
}
});
评论频道
class CommentChannel < ApplicationCable::Channel
def subscribed
# stream_from "message_comments"
# params['message_id'] should get passed in as subscription.create param
stream_from "message:#{ params['message_id'] }:comments"
end
def unsubscribed
end
end
评论工作
class CommentRelayJob < ApplicationJob
def perform(comment)
ActionCable.server.broadcast("message:#{ comment.message_id }:comments", comment: CommentsController.render(partial: 'comments/comment', locals: { comment: comment }))
end
end
上面的 get_id 失败了
comment_channel.js:6 Uncaught ReferenceError: $ is not defined
如何限制对特定消息的订阅?