Rails + ActionCable + Passenger + AWS Elasticache (Redis) : how to fix 'WebSocket is closed before the connection is established.' on production?
Asked Answered
A

1

6

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.3.0
  • Rails 5.0.7.2
  • Nginx 1.10.1
  • Passenger 5.0.30
  • Redis 5.0.3 (on AWS Elasticache)

nginx.conf

server {
  listen 443 ssl;
  server_name myapp.com *myapp.com;
  root /home/ubuntu/myapp/current/public;
  ssl_certificate ...;
  ssl_certificate_key ...;
  passenger_enabled on;
  passenger_min_instances 1;
  ...

  location /cable {
    passenger_app_group_name my_app_action_cable;
    passenger_force_max_concurrent_requests_per_process 0;
  }
}

cable.yml

production:
  adapter: redis
  url: redis://myapp.grzdsm.0001.euw1.cache.amazonaws.com:6379

production.rb

config.action_cable.url = "wss://myapp.com/cable"
config.action_cable.allowed_request_origins = ["https://myapp.com", "myapp.com"]

routes.rb

mount ActionCable.server => '/cable'

notification_channel.rb

class NotificationChannel < ApplicationCable::Channel

  def subscribed
    stream_from "notification"
  end

  def unsubscribed
  end

end

connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  end
end

channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

cable.js

//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);

notification.coffee

App.notification = App.cable.subscriptions.create "NotificationChannel",
  connected: ->
    console.log("connected");

  disconnected: ->
    console.log("disconnected");

  received: (data) ->
    console.log("received");

My logs shows successfully websocket connection

I, [2019-05-06T11:00:57.688540 #4986]  INFO -- : Finished "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:00:57 +0200
I, [2019-05-06T11:01:09.799045 #4986]  INFO -- : Started GET "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:09 +0200
I, [2019-05-06T11:01:09.799154 #4986]  INFO -- : Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2019-05-06T11:01:09.803010 #4986]  INFO -- : Registered connection (Z2lkOi8veXBqL1VzZXIvMjkx)
I, [2019-05-06T11:01:20.541988 #4986]  INFO -- : Finished "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:20 +0200
I, [2019-05-06T11:01:21.784567 #4986]  INFO -- : Started GET "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:21 +0200
I, [2019-05-06T11:01:21.784672 #4986]  INFO -- : Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2019-05-06T11:01:21.788734 #4986]  INFO -- : Registered connection (Z2lkOi8veXBqL1VzZXIvMjkx)
I, [2019-05-06T11:01:43.768741 #4986]  INFO -- : Finished "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:43 +0200
I, [2019-05-06T11:01:44.784226 #4986]  INFO -- : Started GET "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:44 +0200
I, [2019-05-06T11:01:44.784333 #4986]  INFO -- : Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2019-05-06T11:01:44.788154 #4986]  INFO -- : Registered connection (Z2lkOi8veXBqL1VzZXIvMjkx)

Redis monitor is showing subscribe and unsubscribe

1557133304.788584 [0 172.30.1.177:59731] "subscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133326.654308 [0 172.30.1.177:59731] "unsubscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133327.910010 [0 172.30.1.177:59731] "subscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133349.255254 [0 172.30.1.177:59731] "unsubscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133351.183697 [0 172.30.1.177:59731] "subscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133356.974099 [0 172.30.1.177:59731] "unsubscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133357.707420 [0 172.30.1.177:59731] "subscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133371.886670 [0 172.30.1.177:59731] "unsubscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"

But I'm still getting this error : WebSocket connection to 'wss://myapp.com/cable' failed: WebSocket is closed before the connection is established.

Any clue would be really appreciated, thanks !

Archibaldo answered 6/5, 2019 at 9:29 Comment(3)
I'm unable to connect action cable, it's not calling I, [2019-05-06T11:00:57.688540 #4986] INFO -- : Finished "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:00:57 +0200 I, [2019-05-06T11:01:09.799045 #4986] INFO -- : Started GET "/cable/", while setup is same for production.rb and all. can you tell me why it's not calling /cable with server load.Figural
I am having this same issue with Rails 6 - I'm going to open a new question.Elspeth
Did you ever figure this out? Running into this issue on Rails 7 and ELB and Redis #77491953Fustic
K
0

You might need to change 'wss://...' to 'ws://...' in your config/environments/production.rb file

Keyte answered 3/4, 2020 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.