14

I am using Jelastic for my development environment (not yet in production). My application is running with Unicorn but I discovered websockets with ActionCable and integrated it in my application.

Everything is working fine in local, but when deploying to my Jelastic environment (with the default NGINX/Unicorn configuration), I am getting this message in my javascript console and I see nothing in my access log

WebSocket connection to 'ws://dev.myapp.com:8080/' failed: WebSocket is closed before the connection is established.

I used to have on my local environment and I solved it by adding the needed ActionCable.server.config.allowed_request_origins in my config file. So I double-checked my development config for this and it is ok.

That's why I was wondering if there is something specific for NGINX config, else than what is explained on ActionCable git page

bundle exec puma -p 28080 cable/config.ru

For my application, I followed everything from enter link description here but nothing's mentioned about NGINX configuration

I know that websocket with ActionCable is quite new but I hope someone would be able to give me a lead on that

Many thanks

phyzalis
  • 1,236
  • 1
  • 13
  • 24
  • I'm trying to solve my own problem related to this, but my Rails 5 ActionCable set up also required the `config/redis/cable.yml` to be setup such that the production url was set to something other than the default localhost line. I'm using Heroku for production, and since ActionCable uses Redis, I added the Heroku-Redis addon to my application and used `ENV['REDIS_URL']` from my Heroku environment variables. I read through your question and figured I'd mention what else I had to do beyond what you've tried. Caveat: production only works with 2 devices out of all the ones of tested with :( – Jake Smith Jan 08 '16 at 20:56
  • I have just figured my issue but it took me so much time. And I encountered many issues (I will answer to my question tomorrow). I was needing the config/redis/cable.yml too but I didn't know how to set my password in it (I have just found). I have read it was not easy to configure with Heroku so good luck ;) – phyzalis Jan 08 '16 at 21:03
  • Thanks :) It works for two devices total. Beyond that, nobody can connect to the cable. Once I have it figured out, I'm going to write a blog post I think. Once Rails 5 is out, lots of people will be trying it with Heroku – Jake Smith Jan 08 '16 at 22:00

1 Answers1

23

Ok so I finally managed to fix my issue. Here are the different steps which allowed to make this work:

1.nginx : I don't really know if this is needed but as my application is running with Unicorn, I added this into my nginx conf

upstream websocket {
  server 127.0.0.1:28080;
}

server {
  location /cable/ {
    proxy_pass http://websocket/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
  }
}

And then in my config/environments/development.rb file:

config.action_cable.url = "ws://my.app.com/cable/"

2.Allowed request origin: I have then noticed that my connection was refused even if I was using ActionCable.server.config.allowed_request_origins in my config/environments/development.rb file. I am wondering if this is not due to the development default as http://localhost:3000 as stated in the documentation. So I have added this:

ActionCable.server.config.disable_request_forgery_protection = true

I have not yet a production environment so I am not yet able to test how it will be.

3.Redis password: as stated in the documentation, I was using a config/redis/cable.yml but I was having this error:

Error raised inside the event loop: Replies out of sync: #<RuntimeError: ERR operation not permitted>
/var/www/webroot/ROOT/public/shared/bundle/ruby/2.2.0/gems/em-hiredis-0.3.0/lib/em-hiredis/base_client.rb:130:in `block in connect'

So I understood the way I was setting my password for my redis server was not good.

In fact your have to do something like this:

development:
  <<: *local
  :url: redis://user:password@my.redis.com:6379
  :host: my.redis.com
  :port: 6379

And now everything is working fine and Actioncable is really impressive.

Maybe some of my issues were trivial but I am sharing them and how I resolved them so everyone can pick something if needed

Linus Oleander
  • 17,746
  • 15
  • 69
  • 102
phyzalis
  • 1,236
  • 1
  • 13
  • 24
  • you could change: ```ActionCable.server.config.disable_request_forgery_protection = true``` to ```ActionCable.server.config.allowed_request_origins = %w( ws://my.app.com/ )``` – dane Jan 18 '16 at 16:49
  • I forgot to mention that I tried something similar with my local environment and it was working. But deploying to my dev environment, and putting the dev url, it didn't work. By the way I think you are wrong with your protocol as it should be http:// and not ws:// ? – phyzalis Jan 20 '16 at 07:12
  • Yes I was wrong and currently I am trying to get the same thing to work. I have three environments: -localdev, development (remote) server, production (remote) server and it works fine on localdev however I had to fallback and use ```ActionCable.server.config.disable_request_forgery_protection = true``` on the dev server as I cannot figure out yet why my suggestion to use allowed_request_origins is not working yet. – dane Jan 20 '16 at 14:50
  • I am pretty sure they are forcing ActionCable.server.config.allowed_request_origins to http://localhost:3000 when environment is development (as written in their documentation). – phyzalis Jan 20 '16 at 16:13
  • New latest version of rails has its cable conf in `config/cable.yml` not `config/redis/cable.yml`. – Linus Oleander Jan 31 '16 at 20:11
  • Hi, @phyzalis finally are you running action cable server on Puma or Unicorn? Because the heading still says puma. – sethi Jul 25 '16 at 04:59
  • On Puma for actioncable but my application run on Unicorn – phyzalis Jul 25 '16 at 08:55
  • I get the following when I add your code to `nginx.config` ... `"upstream" directive is not allowed here in /usr/local/etc/nginx/nginx.conf:118` now just to confirm, I am adding it to `/usr/local/etc/nginx/nginx.config` on my mac – Kendall Weihe Jul 27 '16 at 20:00
  • I also get `/ruby/2.2.0/psych/visitors/to_ruby.rb:303:in `block in visit_Psych_Nodes_Alias': Unknown alias: local (Psych::BadAlias)` when I add `<<: *local` to `cable.yml` – Kendall Weihe Jul 27 '16 at 20:02
  • If your mac is only for development, I think you don't need the nginx config. Nginx config is only for your production (or staging server) as I suppose you are using webrick on your mac By the way I am sorry but I am not good enough with nginx conf to help you more – phyzalis Jul 28 '16 at 08:03
  • If you're using the same host to run your actioncable, it's easiest to use config.action_cable.url = "/cable" – Kevin May 10 '17 at 02:58