I have a Rails 5 app which uses Action Cable
for websocket functionality.
In my development environment everything works as expected and the browser clients successfully connect to the Action Cable
channels.
In my production environment Action Cable
was working at some point, but then suddenly stopped functioning without any immediate apparent cause.
If I change the RAILS_ENV
to production
while running the app on my development machine Action Cable
works fine. Something seems different when running the app on the actual production machine although the basic environment is the same.
The specific error I see in the Chrome console:
mydomain.com/:1 WebSocket connection to 'wss://mydomain.com/cable' failed: WebSocket is closed before the connection is established
. I get a similar error in other browsers so it doesn't appear to be browser related. I disabled any adblockers while testing just to be sure they do not interfere.
Development.rb ENV related setup:
config.action_cable.url = "ws://localhost:#{port}/cable"
Production.rb ENV related setup:
hostname = ENV.fetch('HOSTNAME')
port = ENV.fetch('PORT')
base_url = "#{hostname}:#{port}"
config.action_cable.url = "wss://#{hostname}/cable"
config.action_cable.allowed_request_origins = ["https://#{base_url}", "https://#{hostname}"]
I use Puma
as a webserver. The webserver serves a SSL connection for which a valid certificate is installed. On the production machine Puma serves the application on port 3000
but this is forwarded to port 443
in the router.
The only notable difference with running the app on my dev machine and production is that in production SSL is used.