aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-12-13 14:26:46 +0100
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-12-13 14:26:46 +0100
commit7328aa41c56fcb4912c9f6e6cb5fc972e446c5ba (patch)
tree71f2c42e7d94ecec70cf94f0ab363498c88fb8c3 /lib
parent5fec4b96ffadf1624e6840d7446d78dba40add30 (diff)
parent62250e38a1269030e036cf23a6ea6714fb00ffac (diff)
downloadrails-7328aa41c56fcb4912c9f6e6cb5fc972e446c5ba.tar.gz
rails-7328aa41c56fcb4912c9f6e6cb5fc972e446c5ba.tar.bz2
rails-7328aa41c56fcb4912c9f6e6cb5fc972e446c5ba.zip
Merge pull request #73 from smellsblue/websocket-url-differentiation
Easy websocket url configuration
Diffstat (limited to 'lib')
-rw-r--r--lib/action_cable/engine.rb5
-rw-r--r--lib/action_cable/helpers/action_cable_helper.rb29
-rw-r--r--lib/action_cable/server/configuration.rb1
-rw-r--r--lib/assets/javascripts/cable.coffee.erb6
4 files changed, 40 insertions, 1 deletions
diff --git a/lib/action_cable/engine.rb b/lib/action_cable/engine.rb
index 613a9b99f2..4777c3886b 100644
--- a/lib/action_cable/engine.rb
+++ b/lib/action_cable/engine.rb
@@ -1,10 +1,15 @@
require 'rails/engine'
require 'active_support/ordered_options'
+require 'action_cable/helpers/action_cable_helper'
module ActionCable
class Engine < ::Rails::Engine
config.action_cable = ActiveSupport::OrderedOptions.new
+ config.to_prepare do
+ ApplicationController.helper ActionCable::Helpers::ActionCableHelper
+ end
+
initializer "action_cable.logger" do
ActiveSupport.on_load(:action_cable) { self.logger ||= ::Rails.logger }
end
diff --git a/lib/action_cable/helpers/action_cable_helper.rb b/lib/action_cable/helpers/action_cable_helper.rb
new file mode 100644
index 0000000000..b82751468a
--- /dev/null
+++ b/lib/action_cable/helpers/action_cable_helper.rb
@@ -0,0 +1,29 @@
+module ActionCable
+ module Helpers
+ module ActionCableHelper
+ # Returns an "action-cable-url" meta tag with the value of the url specified in your
+ # configuration. Ensure this is above your javascript tag:
+ #
+ # <head>
+ # <%= action_cable_meta_tag %>
+ # <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
+ # </head>
+ #
+ # This is then used by ActionCable to determine the url of your websocket server.
+ # Your CoffeeScript can then connect to the server without needing to specify the
+ # url directly:
+ #
+ # #= require cable
+ # @App = {}
+ # App.cable = Cable.createConsumer()
+ #
+ # Make sure to specify the correct server location in each of your environments
+ # config file:
+ #
+ # config.action_cable.url = "ws://example.com:28080"
+ def action_cable_meta_tag
+ tag "meta", name: "action-cable-url", content: Rails.application.config.action_cable.url
+ end
+ end
+ end
+end
diff --git a/lib/action_cable/server/configuration.rb b/lib/action_cable/server/configuration.rb
index 89a0caddb4..f7fcee019b 100644
--- a/lib/action_cable/server/configuration.rb
+++ b/lib/action_cable/server/configuration.rb
@@ -9,6 +9,7 @@ module ActionCable
attr_accessor :connection_class, :worker_pool_size
attr_accessor :redis_path, :channels_path
attr_accessor :disable_request_forgery_protection, :allowed_request_origins
+ attr_accessor :url
def initialize
@logger = Rails.logger
diff --git a/lib/assets/javascripts/cable.coffee.erb b/lib/assets/javascripts/cable.coffee.erb
index 8498233c11..25a9fc79c2 100644
--- a/lib/assets/javascripts/cable.coffee.erb
+++ b/lib/assets/javascripts/cable.coffee.erb
@@ -4,5 +4,9 @@
@Cable =
INTERNAL: <%= ActionCable::INTERNAL.to_json %>
- createConsumer: (url) ->
+ createConsumer: (url = @getConfig("url")) ->
new Cable.Consumer url
+
+ getConfig: (name) ->
+ element = document.head.querySelector("meta[name='action-cable-#{name}']")
+ element?.getAttribute("content")