I have just started to explore RoR and not able to setup a websocket test connection which shows in browser console logs as “connected”.
I have setup the things with ActionCable like routes and creating a channel. How do I test the websocket connection? I just see nothing in developer tools which means client is not able to connect to websocket.
Here are my setup files:
route file:
mount ActionCable.server => '/cable'
cat config/cable.yml
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: my_app_production
production.rb file
config.action_cable.url = "wss://192.168.122.162/cable"
config.action_cable.allowed_request_origins = [ "http://192.168.122.162" ]
app/channels/messages_channel.rb
class MessagesChannel < ApplicationCable::Channel
def subscribed
stream_from 'MessagesChannel'
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
app/javascript/channels/consumer.js
import { createConsumer } from "@rails/actioncable"
//export default createConsumer()
export default createConsumer("wss://192.168.122.162/cable")
app/javascript/channels/messages_channel.js
import consumer from "channels/consumer"
consumer.subscriptions.create("MessagesChannel", {
connected() {
// Called when the subscription is ready for use on the server
console.log('Connected');
},
disconnected() {
// Called when the subscription has been terminated by the server
},
received(data) {
// Called when there's incoming data on the websocket for this channel
}
});