问题
I am facing a problem while working with Action Cable, whenever I run my program I receive an error that says Subscription
Class not found ConversationChannel
and when I try to send a message I get this log
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket) Subscription class not found: "ConversationChannel" Could not execute command from {"command"=>"message", "identifier"=>"{\"channel\":\"ConversationChannel\"}", "data"=>"{\"message\":[{\"name\":\"conversation_id\",\"value\":\"2\"},{\"name\":\"amitian_id\",\"value\":\"1\"},{\"name\":\"body\",\"value\":\"nmm\"}],\"action\":\"speak\"}"}) [RuntimeError - Unable to find subscription with identifier: {"channel":"ConversationChannel"}]: C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:74:in
find' | C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:53:in
perform_action' | C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:17:inexecute_command' | C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/base.rb:88:in
dispatch_websocket_message' | C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/server/worker.rb:58:in `block in invoke'
ConversationChannel.rb
class ConversationChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
stream_from "conversations-#{current_amitian.id}"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
stop_all_streams
end
def speak(data)
message_params = data['message'].each_with_object({}) do |el, hash|
hash[el.values.first] = el.values.last
end
ActionCable.server.broadcast(
"conversations-#{current_amitian.id}",
message: message_params
)
end
end
conversation.js
App.conversation = App.cable.subscriptions.create("ConversationChannel", {
connected: function() {
},
disconnected: function() {
},
received: function(data) {
console.log(data['message']);
},
speak: function(message) {
return this.perform('speak' , {
message: message
});
}
});
$(document).on('submit', '.new_message', function(e) {
e.preventDefault();
var values = $(this).serializeArray();
App.conversation.speak(values);
$(this).trigger('reset');
});
connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_amitian
def connect
self.current_amitian = find_verified_amitian
end
protected
def find_verified_amitian
if(current_amitian = env['warden'].amitian)
current_amitian
else
reject_unauthorized_connection
end
end
end
end
using the log file given above can anyone tell me why It can't find my conversation_channel.rb
file ?
回答1:
I have resolved it by moving the channel file up one directory.
From app/channels/application_cable/conversation_channel.rb
to app/channels/conversation_channel.rb
回答2:
You have to provide the full Module::Class name in your client (JS) code. For e.g. if your ConversationChannel
class is inside ApplicationCable
module then in your client instead of just ConversationChannel
you need to do ApplicationCable::ConversationChannel
回答3:
Your file needs to named as conversation_channel.rb
, as snake_case is the rails convention for channel file names.
来源:https://stackoverflow.com/questions/44486823/subscription-class-not-found-mychannel-in-actioncable