Websockets and Rails

Make WebSocket work with Ruby on Rails vs Puma and Nginx

The problem was in the config/application.rb file. I've set correctly the following config option: config.action_cable.url = https://test.mysite.com however didn't set the config.action_cable.allowed_request_origins

Correct config/application.rb file should look like this:

require 'active_model/railtie'
require 'active_record/railtie'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_view/railtie'
require 'action_cable/engine'
require 'sprockets/railtie'

Bundler.require(*Rails.groups)

module MyModule
class Application < Rails::Application

...

config.action_cable.url = https://test.mysite.com
config.action_cable.allowed_request_origins = [/https:\/\/test.mysite.com/]

...

end
end

How to connect to a websocket API with Rails?

ActionCable is for setting up websocket servers. It sounds like you are looking for a websocket client. You could check out this gem as a starting point: https://github.com/shokai/websocket-client-simple

Rails 6 ActionCable Unable to Upgrade WebSocket Request

After posting on reddit, I was able to fix my issue by:

  1. Removing my .ebextensions/nginx_proxy.config file.
  2. Creating a new file, .platform/nginx/conf.d/elasticbeanstalk/websocket.conf with the contents:
location /cable {
proxy_pass http://my_app/cable;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Rails and WebSockets using SSL/TLS in development environment

The solution was to visit https://my.app.eu:28080/ and accept the certificate, then the WSS connections are working.



Related Topics



Leave a reply



Submit