前提・実現したいこと
RailsでAction Cableを使用したアプリケーションを作りたい。
発生している問題・エラーメッセージ
何故かsubscriptionsのreceivedメソッドが実行されない。
該当のソースコード
とりあえず実行したコマンド、記述したソースコードを全部書きます。
というか、この記事の内容を丸コピです。
Action Cableでリアルタイムチャットアプリの作成方法
↓コマンド
$ rails new ac_test $ rails g controller rooms show $ rails g model message content:text $ rails db:migrate $ rails g channel room speak
↓app/channels/room_channel.rb
ruby
1class RoomChannel < ApplicationCable::Channel 2 def subscribed 3 stream_from "room_channel" 4 end 5 6 def unsubscribed 7 # Any cleanup needed when channel is unsubscribed 8 end 9 10 def speak(data) 11 ActionCable.server.broadcast 'room_channel', message: data['message'] 12 end 13end
↓app/assets/javascripts/channels/room.coffee
coffeescript
1App.chat_message = App.cable.subscriptions.create "RoomChannel", 2 connected: -> 3 # Called when the subscription is ready for use on the server 4 5 disconnected: -> 6 # Called when the subscription has been terminated by the server 7 8 received: (data) -> 9 # Called when there's incoming data on the websocket for this channel 10 alert data['message'] 11 12 speak: (message) -> 13 @perform 'speak', message: message
上記の記事では、この時点でブラウザからコンソールにApp.room.speak("test")
と入力すればアラートが表示されると記載されていたのですが、全く表示されませんでした。
自分では原因が全くわからないので、困り果てています。。。
補足情報(FW/ツールのバージョンなど)
Rails 5.2.2
Ruby 2.6.0
sqlite3 1.3.6
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。