14
votes

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

1
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
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
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 HerokuJake Smith

1 Answers

21
votes

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:[email protected]: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