Websocket is set up and running in a Rails 7.x app and everything is working find. But when I try to connect to rails websocket from the outside (javascript) i get a message like this:
WebSocket connection to 'ws://192.168.178.159:3000/cable' failed: actioncable.esm-06609b0ecaffe2ab952021b9c8df8b6c68f65fc23bee728fc678a2605e1ce132.js:165
The error message is copied from chrom console. The javascript code looks like this:
function connectWebSocket(socketUrl) {
socket = new WebSocket(socketUrl);
// WebSocket event listeners
socket.onopen = function() {
console.log('WebSocket connection established.');
};
socket.onmessage = function(event) {
console.log('Received message:', event.data);
};
socket.onclose = function() {
console.log('WebSocket connection closed.');
};
}
app/channels/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def receive(data)
ActionCable.server.broadcast "chat", message: data["message"]
end
end
config/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins "*"
resource "*",
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
config/development.rb
...
config.action_cable.disable_request_forgery_protection = true
config.action_cable.mount_path = '/cable'
Im starting the app like this:
rails s -b 0.0.0.0
I’m running out of ideas how to fix this.