Rails6 + Nginx + Passenger + ActionCable = websocket is closed

Viewed 642

There is a similar - unanswered - question for Rails 5, but, as I am using Rails 6 I am asking again.

I have some troubles working my action cable in production environment. Development env is working fine.

Error to solve : WebSocket connection to 'wss://myapp.com/cable' failed: WebSocket is closed before the connection is established.

I'm getting this error multiple times on Chrome's console.

Stack

  • Ruby 2.6.5
  • Rails 6.0.3.2
  • Nginx 1.14.0

nginx.conf

server {
listen 443 ssl;

  server_name *.myapp.com;
ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem;

  root /home/deploy/myapp/current/public;

  passenger_enabled on;
  passenger_app_env production;

  location /cable {
    passenger_app_group_name myapp_websocket;
    passenger_force_max_concurrent_requests_per_process 0;
  }

  # Allow uploads up to 100MB in size
  client_max_body_size 100m;

  location ~ ^/(assets|packs) {
    expires max;
    gzip_static on;
  }
}

cable.yml

development:
  adapter: redis

test:
  adapter: test

production:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
  channel_prefix: myapp_production

production.rb

  config.action_cable.url = 'wss://app.myapp.com/cable'
  config.action_cable.allowed_request_origins = [ 'https://app.myapp.com' ]

These lines constantly repeat in the log: log/production.log

I, [2020-09-15T13:05:02.679580 #32158]  INFO -- : Finished "/cable/" [WebSocket] for xxx.xxx.133.88 at 2020-09-15 13:05:02 +0000
I, [2020-09-15T13:05:03.791781 #32158]  INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Started GET "/cable" for xxx.xxx.133.88 at 2020-09-15 13:05:03 +0000
I, [2020-09-15T13:05:03.792248 #32158]  INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Started GET "/cable/" [WebSocket] for xxx.xxx.133.88 at 2020-09-15 13:05:03 +0000
I, [2020-09-15T13:05:03.792320 #32158]  INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPG

browser console

WebSocket connection to 'wss://app.myapp.com/cable' failed: WebSocket is closed before the connection is established.
1 Answers

Can you please comment this line in your production.rb

config.action_cable.url = 'wss://app.myapp.com/cable'

Also, make sure to have this line in your view layout file default name is application.html.erb.

Add this in the head section.

<%= action_cable_meta_tag %>

Documentation states that:

8.3 Consumer Configuration

To configure the URL, add a call to action_cable_meta_tag in your HTML layout HEAD. This uses a URL or path typically set via config.action_cable.url in the environment configuration files.

Why this is needed in production and not development is not explained or clear.

Related