ActionCable Rails 5 (Passenger) - failed: Error during WebSocket handshake: Unexpected response code: 404
Asked Answered
B

2

6

I am trying to deploy ActionCable and Rails 5 To Production server (DigitalOcean). I have followed all steps mention in the Gorails video here: https://gorails.com/episodes/deploy-actioncable-and-rails-5

The app was deployed using Phusion Passenger + Nginx + Capistrano.

And when I tried to checked my site on production, I got this error on the browser's console:

WebSocket connection to 'ws://139.59.175.34/cable' failed: Error during WebSocket handshake: Unexpected response code: 404

These are my settings:

/etc/nginx/sites-enabled/default

server {
        listen 80;
        listen [::]:80 ipv6only=on;

        server_name my_server_domain;
        passenger_enabled on;
        rails_env    production;
        root         /home/deploy/my_app_domain/current/public;

        # ActionCabel config (disable this if u r not using it)
        location /cable {
           passenger_app_group_name actioncable_websocket;
           passenger_force_max_concurrent_requests_per_process 0;
        }

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

production.rb

config.action_cable.url = "/cable"
config.action_cable.allowed_request_origins = ["http://139.59.175.34"]

cable.js

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

  App.cable = ActionCable.createConsumer("/cable");

}).call(this);

I have tried to change the ActionCable config on config/production.rb to:

  config.action_cable.url = [/ws:\/\/*/, /wss:\/\/*/]
  config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]

but still no luck.


I have also looked into the production.log on the server and this is the error that was recorded:

WebSocket error occurred: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0

UPDATE:

Below is firewall setting on the server:

enter image description here

Baskerville answered 4/9, 2018 at 23:34 Comment(2)
What is the operating system? Have you checked the firewall? It looks like the socket bit is already reserved by another running software.Apollinaire
@Apollinaire The operating system is Linux 18.04. And yes, the firewall is inactive. Please refer to the update on my post above.Baskerville
B
1

Finally I found the answer to my problem. It turns out that I do not have redis installed on my production server. So I just need to install redis and now everything works great!

To check whether you have redis installed on the server or not, use this command:

redis-cli

If the redis console appear, then you're all set. If an error/warning comes out, you can follow these steps to install and run redis:

  1. sudo apt install redis-server
  2. redis-server

Hope it helps!

Baskerville answered 24/10, 2018 at 23:41 Comment(1)
any idea about how to install it on heroku?Zepeda
A
0

Your configuration looks correct.

This issue can happens if you have a firewall running.

Check with your iptables configuration (linux) if something is blocking it (I don't have a specific line of code to give to you though).

If you're on Windows, check that there are no "antivirus" or "firewall" intercepting / blocking the bits. I heard that Node32 is a very good candidate to block websockets.

Update: Can you also try to connect using wss://139.59.175.34/cable instead of ws://139.59.175.34/cable ? (Be sure your NGinx is proxying with SSL). https://docs.nginx.com/nginx/admin-guide/security-controls/securing-http-traffic-upstream/

Apollinaire answered 7/9, 2018 at 15:40 Comment(2)
The operating system is Linux 18.04. And the firewall is inactive. Please refer to the update on my post above. Also I have tried it with the SSL setting, same thing, it's not working.Baskerville
@RizalYusoff I'm out of idea for now. I will come back to this if I have more.Apollinaire

© 2022 - 2024 — McMap. All rights reserved.