Deploy ActionCable in production
A guide on how to deploy ActionCable in a production environment
Overview
ActionCable is the new websocket implementation included in Rails 5. Here's how to deploy it to production! Please note a lot of this is changing quickly so not everything may work as expected.
Ubuntu
This section assumes you've already setup an Ubuntu server using the Deploy Rails on Ubuntu guide.
Our guide takes you through installing Passenger 5 but if you've installed it previous, you'll need to update to the latest version (5.0.24+) as it has been updated to work with Rails 5 and ActionCable.
ActionCable relies upon having a websocket server running and a pubsub server. For the pubsub connection, you can use either Redis or PostgreSQL as easy options to get started. It's recommended to use Redis for this because you're likely to be using it for background jobs anyways.
Install Redis
First we'll start by installing Redis server that will run locally on the server:
sudo apt-get install redis-server
Configure Your App
Next we'll modify our Rails app's configuration to use a mounted ActionCable server inside the routes.
Add the following to your config/routes.rb
file at the top:
mount ActionCable.server => "/cable"
Modify your ActionCable Javascript to use that route in your app/assets/javascripts/channels/index.coffee
file:
App.cable = ActionCable.createConsumer("/cable")
Set the allowed origin domains that you want to allow on websockets inside config/environments/production.rb
. This should just be the domains your website runs on.
config.action_cable.allowed_request_origins = ["http://yourdomain.com"]
Finally, deploy your modified code
git commit -m "Configured ActionCable"
cap production deploy # Assuming you're using Capistrano
Configure Nginx & Passenger
Last but not least, you'll need to add a snippet to your Nginx config in /etc/nginx/sites-enabled/default
:
server {
listen 80;
server_name yourdomain.com;
passenger_enabled on;
rails_env production;
root /home/deploy/your_app/current/public;
location /cable {
passenger_app_group_name your_app_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
And restart Nginx!
sudo service nginx restart
Check out your site and you should have everything working! If you run into any issues, check your Rails and Nginx server logs for more information on the errors.