From d67ff329cfdc75e27e58b92f772b3b4059327598 Mon Sep 17 00:00:00 2001 From: Kapil Chouhan Date: Sun, 8 Mar 2020 21:14:31 +0530 Subject: [PATCH] added middleware for run Websockets with ActionCable on production Signed-off-by: Kapil Chouhan --- app/middleware/chat_action_cable.rb | 13 +++++++++++++ config/environments/production.rb | 3 +++ 2 files changed, 16 insertions(+) create mode 100644 app/middleware/chat_action_cable.rb diff --git a/app/middleware/chat_action_cable.rb b/app/middleware/chat_action_cable.rb new file mode 100644 index 0000000..297db2f --- /dev/null +++ b/app/middleware/chat_action_cable.rb @@ -0,0 +1,13 @@ +class ChatActionCable < ActionCable::Connection::WebSocket + def initialize(app, options={}) + @app = app + end + + def call(env) + if ::WebSocket::Driver.websocket?(env) + ActionCable.server.call(env) + else + @app.call(env) + end + end +end diff --git a/config/environments/production.rb b/config/environments/production.rb index 54cb92d..2129e26 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -14,6 +14,9 @@ config.consider_all_requests_local = false config.action_controller.perform_caching = true + config.middleware.use ChatActionCable + config.web_socket_server_url = "wss://rails-web-sockets.herokuapp.com/" + # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). # config.require_master_key = true