aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md41
-rw-r--r--lib/action_cable.rb10
-rw-r--r--lib/action_cable/channel/base.rb7
-rw-r--r--lib/action_cable/connection/base.rb10
-rw-r--r--lib/action_cable/connection/tagged_logger_proxy.rb12
-rw-r--r--lib/action_cable/server/worker/active_record_connection_management.rb2
-rw-r--r--lib/assets/javascripts/cable.coffee11
-rw-r--r--lib/assets/javascripts/cable.coffee.erb8
-rw-r--r--lib/assets/javascripts/cable/connection.coffee15
-rw-r--r--lib/assets/javascripts/cable/connection_monitor.coffee2
-rw-r--r--lib/assets/javascripts/cable/subscriptions.coffee2
11 files changed, 75 insertions, 45 deletions
diff --git a/README.md b/README.md
index 5c6cb1c0fe..ebc505db19 100644
--- a/README.md
+++ b/README.md
@@ -54,7 +54,7 @@ module ApplicationCable
protected
def find_verified_user
- if current_user = User.find(cookies.signed[:user_id])
+ if current_user = User.find_by(id: cookies.signed[:user_id])
current_user
else
reject_unauthorized_connection
@@ -162,7 +162,7 @@ $(document).on 'click', '[data-behavior~=appear_away]', ->
```
Simply calling `App.cable.subscriptions.create` will setup the subscription, which will call `AppearanceChannel#subscribed`,
-which in turn is linked to original `App.consumer` -> `ApplicationCable::Connection` instances.
+which in turn is linked to original `App.cable` -> `ApplicationCable::Connection` instances.
We then link `App.appearance#appear` to `AppearanceChannel#appear(data)`. This is possible because the server-side
channel instance will automatically expose the public methods declared on the class (minus the callbacks), so that these
@@ -187,7 +187,7 @@ class WebNotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from "web_notifications_#{current_user.id}"
end
- end
+end
```
```coffeescript
@@ -219,14 +219,14 @@ class ChatChannel < ApplicationCable::Channel
def subscribed
stream_from "chat_#{params[:room]}"
end
- end
+end
```
Pass an object as the first argument to `subscriptions.create`, and that object will become your params hash in your cable channel. The keyword `channel` is required.
```coffeescript
# Client-side which assumes you've already requested the right to send web notifications
-App.cable.subscriptions.create {channel: "ChatChannel", room: "Best Room"},
+App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" },
received: (data) ->
new Message data['sent_by'], body: data['body']
```
@@ -257,11 +257,11 @@ end
```coffeescript
# Client-side which assumes you've already requested the right to send web notifications
-sub = App.cable.subscriptions.create {channel: "ChatChannel", room: "Best Room"},
+sub = App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" },
received: (data) ->
new Message data['sent_by'], body: data['body']
-sub.send {sent_by: 'Peter', body: 'Hello Paul, thanks for the compliment.'}
+sub.send { sent_by: 'Peter', body: 'Hello Paul, thanks for the compliment.' }
```
The rebroadcast will be received by all connected clients, _including_ the client that sent the message. Note that params are the same as they were when you subscribed to the channel.
@@ -274,8 +274,11 @@ See the [rails/actioncable-examples](http://github.com/rails/actioncable-example
## Configuration
-The only must-configure part of Action Cable is the Redis connection. By default, `ActionCable::Server::Base` will look for a configuration
-file in `Rails.root.join('config/redis/cable.yml')`. The file must follow the following format:
+Action Cable has two required configurations: the Redis connection and specifying allowed request origins.
+
+### Redis
+
+By default, `ActionCable::Server::Base` will look for a configuration file in `Rails.root.join('config/redis/cable.yml')`. The file must follow the following format:
```yaml
production: &production
@@ -299,6 +302,24 @@ a Rails initializer with something like:
ActionCable.server.config.redis_path = Rails.root('somewhere/else/cable.yml')
```
+### Allowed Request Origins
+
+Action Cable will only accepting requests from specified origins, which are passed to the server config as an array:
+
+```ruby
+ActionCable.server.config.allowed_request_origins = %w( http://rubyonrails.com )
+```
+
+To disable and allow requests from any origin:
+
+```ruby
+ActionCable.server.config.disable_request_forgery_protection = true
+```
+
+By default, Action Cable allows all requests from localhost:3000 when running in the development environment.
+
+### Other Configurations
+
The other common option to configure is the log tags applied to the per-connection logger. Here's close to what we're using in Basecamp:
```ruby
@@ -416,4 +437,4 @@ Action Cable is released under the MIT license:
Bug reports can be filed for the alpha development project here:
-* https://github.com/rails/actioncable/issues
+* https://github.com/rails/actioncable/issues \ No newline at end of file
diff --git a/lib/action_cable.rb b/lib/action_cable.rb
index 89ffa1fda7..3919812161 100644
--- a/lib/action_cable.rb
+++ b/lib/action_cable.rb
@@ -5,6 +5,16 @@ require 'action_cable/version'
module ActionCable
extend ActiveSupport::Autoload
+ INTERNAL = {
+ identifiers: {
+ ping: '_ping'.freeze
+ },
+ message_types: {
+ confirmation: 'confirm_subscription'.freeze,
+ rejection: 'reject_subscription'.freeze
+ }
+ }
+
# Singleton instance of the server
module_function def server
@server ||= ActionCable::Server::Base.new
diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb
index b0e112ce38..ca903a810d 100644
--- a/lib/action_cable/channel/base.rb
+++ b/lib/action_cable/channel/base.rb
@@ -89,9 +89,6 @@ module ActionCable
include Naming
include Broadcasting
- SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE = 'confirm_subscription'.freeze
- SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE = 'reject_subscription'.freeze
-
attr_reader :params, :connection, :identifier
delegate :logger, to: :connection
@@ -258,7 +255,7 @@ module ActionCable
def transmit_subscription_confirmation
unless subscription_confirmation_sent?
logger.info "#{self.class.name} is transmitting the subscription confirmation"
- connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE)
+ connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: ActionCable::INTERNAL[:message_types][:confirmation])
@subscription_confirmation_sent = true
end
end
@@ -270,7 +267,7 @@ module ActionCable
def transmit_subscription_rejection
logger.info "#{self.class.name} is transmitting the subscription rejection"
- connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE)
+ connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: ActionCable::INTERNAL[:message_types][:rejection])
end
end
end
diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb
index a988060d56..b93b6a8a50 100644
--- a/lib/action_cable/connection/base.rb
+++ b/lib/action_cable/connection/base.rb
@@ -56,7 +56,7 @@ module ActionCable
def initialize(server, env)
@server, @env = server, env
- @logger = new_tagged_logger || server.logger
+ @logger = new_tagged_logger
@websocket = ActionCable::Connection::WebSocket.new(env)
@subscriptions = ActionCable::Connection::Subscriptions.new(self)
@@ -119,7 +119,7 @@ module ActionCable
end
def beat
- transmit ActiveSupport::JSON.encode(identifier: '_ping', message: Time.now.to_i)
+ transmit ActiveSupport::JSON.encode(identifier: ActionCable::INTERNAL[:identifiers][:ping], message: Time.now.to_i)
end
@@ -194,10 +194,8 @@ module ActionCable
# Tags are declared in the server but computed in the connection. This allows us per-connection tailored tags.
def new_tagged_logger
- if server.logger.respond_to?(:tagged)
- TaggedLoggerProxy.new server.logger,
- tags: server.config.log_tags.map { |tag| tag.respond_to?(:call) ? tag.call(request) : tag.to_s.camelize }
- end
+ TaggedLoggerProxy.new server.logger,
+ tags: server.config.log_tags.map { |tag| tag.respond_to?(:call) ? tag.call(request) : tag.to_s.camelize }
end
def started_request_message
diff --git a/lib/action_cable/connection/tagged_logger_proxy.rb b/lib/action_cable/connection/tagged_logger_proxy.rb
index 34063c1d42..e5319087fb 100644
--- a/lib/action_cable/connection/tagged_logger_proxy.rb
+++ b/lib/action_cable/connection/tagged_logger_proxy.rb
@@ -16,6 +16,15 @@ module ActionCable
@tags = @tags.uniq
end
+ def tag(logger)
+ if logger.respond_to?(:tagged)
+ current_tags = tags - logger.formatter.current_tags
+ logger.tagged(*current_tags) { yield }
+ else
+ yield
+ end
+ end
+
%i( debug info warn error fatal unknown ).each do |severity|
define_method(severity) do |message|
log severity, message
@@ -24,8 +33,7 @@ module ActionCable
protected
def log(type, message)
- current_tags = tags - @logger.formatter.current_tags
- @logger.tagged(*current_tags) { @logger.send type, message }
+ tag(@logger) { @logger.send type, message }
end
end
end
diff --git a/lib/action_cable/server/worker/active_record_connection_management.rb b/lib/action_cable/server/worker/active_record_connection_management.rb
index 1ede0095f8..ecece4e270 100644
--- a/lib/action_cable/server/worker/active_record_connection_management.rb
+++ b/lib/action_cable/server/worker/active_record_connection_management.rb
@@ -12,7 +12,7 @@ module ActionCable
end
def with_database_connections
- ActiveRecord::Base.logger.tagged(*connection.logger.tags) { yield }
+ connection.logger.tag(ActiveRecord::Base.logger) { yield }
ensure
ActiveRecord::Base.clear_active_connections!
end
diff --git a/lib/assets/javascripts/cable.coffee b/lib/assets/javascripts/cable.coffee
deleted file mode 100644
index fca5e095b5..0000000000
--- a/lib/assets/javascripts/cable.coffee
+++ /dev/null
@@ -1,11 +0,0 @@
-#= require_self
-#= require cable/consumer
-
-@Cable =
- PING_IDENTIFIER: "_ping"
- INTERNAL_MESSAGES:
- SUBSCRIPTION_CONFIRMATION: 'confirm_subscription'
- SUBSCRIPTION_REJECTION: 'reject_subscription'
-
- createConsumer: (url) ->
- new Cable.Consumer url
diff --git a/lib/assets/javascripts/cable.coffee.erb b/lib/assets/javascripts/cable.coffee.erb
new file mode 100644
index 0000000000..8498233c11
--- /dev/null
+++ b/lib/assets/javascripts/cable.coffee.erb
@@ -0,0 +1,8 @@
+#= require_self
+#= require cable/consumer
+
+@Cable =
+ INTERNAL: <%= ActionCable::INTERNAL.to_json %>
+
+ createConsumer: (url) ->
+ new Cable.Consumer url
diff --git a/lib/assets/javascripts/cable/connection.coffee b/lib/assets/javascripts/cable/connection.coffee
index b6b99413dc..b2abe8dcb2 100644
--- a/lib/assets/javascripts/cable/connection.coffee
+++ b/lib/assets/javascripts/cable/connection.coffee
@@ -1,4 +1,7 @@
# Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation.
+
+{message_types} = Cable.INTERNAL
+
class Cable.Connection
@reopenDelay: 500
@@ -54,17 +57,13 @@ class Cable.Connection
message: (event) ->
{identifier, message, type} = JSON.parse(event.data)
- if type?
- @handleTypeMessage(type)
- else
- @consumer.subscriptions.notify(identifier, "received", message)
-
- onTypeMessage: (type) ->
switch type
- when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION
+ when message_types.confirmation
@consumer.subscriptions.notify(identifier, "connected")
- when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_REJECTION
+ when message_types.rejection
@consumer.subscriptions.reject(identifier)
+ else
+ @consumer.subscriptions.notify(identifier, "received", message)
open: ->
@disconnected = false
diff --git a/lib/assets/javascripts/cable/connection_monitor.coffee b/lib/assets/javascripts/cable/connection_monitor.coffee
index bf99dee34d..435efcc361 100644
--- a/lib/assets/javascripts/cable/connection_monitor.coffee
+++ b/lib/assets/javascripts/cable/connection_monitor.coffee
@@ -7,7 +7,7 @@ class Cable.ConnectionMonitor
@staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings)
- identifier: Cable.PING_IDENTIFIER
+ identifier: Cable.INTERNAL.identifiers.ping
constructor: (@consumer) ->
@consumer.subscriptions.add(this)
diff --git a/lib/assets/javascripts/cable/subscriptions.coffee b/lib/assets/javascripts/cable/subscriptions.coffee
index 13db32eb2c..7955565f06 100644
--- a/lib/assets/javascripts/cable/subscriptions.coffee
+++ b/lib/assets/javascripts/cable/subscriptions.coffee
@@ -63,7 +63,7 @@ class Cable.Subscriptions
sendCommand: (subscription, command) ->
{identifier} = subscription
- if identifier is Cable.PING_IDENTIFIER
+ if identifier is Cable.INTERNAL.identifiers.ping
@consumer.connection.isOpen()
else
@consumer.send({command, identifier})