Skip to content

Commit d3f0aa3

Browse files
committed
Merge pull request rails#23668 from maclover7/cable-docs
Full Action Cable documentation read through
2 parents b86bf39 + 05088b6 commit d3f0aa3

File tree

16 files changed

+67
-70
lines changed

16 files changed

+67
-70
lines changed

actioncable/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ reflections of each unit.
3939
### A full-stack example
4040

4141
The first thing you must do is define your `ApplicationCable::Connection` class in Ruby. This
42-
is the place where you authorize the incoming connection, and proceed to establish it
42+
is the place where you authorize the incoming connection, and proceed to establish it,
4343
if all is well. Here's the simplest example starting with the server-side connection class:
4444

4545
```ruby
@@ -73,7 +73,7 @@ use that to set the `current_user`. By identifying the connection by this same c
7373
you're also ensuring that you can later retrieve all open connections by a given user (and
7474
potentially disconnect them all if the user is deleted or deauthorized).
7575

76-
Then you should define your `ApplicationCable::Channel` class in Ruby. This is the place where you put
76+
Next, you should define your `ApplicationCable::Channel` class in Ruby. This is the place where you put
7777
shared logic between your channels.
7878

7979
```ruby
@@ -94,7 +94,7 @@ The client-side needs to setup a consumer instance of this connection. That's do
9494
App.cable = ActionCable.createConsumer("ws://cable.example.com")
9595
```
9696

97-
The ws://cable.example.com address must point to your set of Action Cable servers, and it
97+
The `ws://cable.example.com` address must point to your Action Cable server(s), and it
9898
must share a cookie namespace with the rest of the application (which may live under http://example.com).
9999
This ensures that the signed cookie will be correctly sent.
100100

@@ -105,8 +105,8 @@ is defined by declaring channels on the server and allowing the consumer to subs
105105

106106
### Channel example 1: User appearances
107107

108-
Here's a simple example of a channel that tracks whether a user is online or not and what page they're on.
109-
(This is useful for creating presence features like showing a green dot next to a user name if they're online).
108+
Here's a simple example of a channel that tracks whether a user is online or not, and also what page they are currently on.
109+
(This is useful for creating presence features like showing a green dot next to a user's name if they're online).
110110

111111
First you declare the server-side channel:
112112

@@ -180,7 +180,7 @@ App.cable.subscriptions.create "AppearanceChannel",
180180
Simply calling `App.cable.subscriptions.create` will setup the subscription, which will call `AppearanceChannel#subscribed`,
181181
which in turn is linked to original `App.cable` -> `ApplicationCable::Connection` instances.
182182

183-
We then link the client-side `appear` method to `AppearanceChannel#appear(data)`. This is possible because the server-side
183+
Next, we link the client-side `appear` method to `AppearanceChannel#appear(data)`. This is possible because the server-side
184184
channel instance will automatically expose the public methods declared on the class (minus the callbacks), so that these
185185
can be reached as remote procedure calls via a subscription's `perform` method.
186186

@@ -215,7 +215,7 @@ ActionCable.server.broadcast \
215215
"web_notifications_#{current_user.id}", { title: 'New things!', body: 'All the news that is fit to print' }
216216
```
217217

218-
The `ActionCable.server.broadcast` call places a message in the Redis' pubsub queue under a separate broadcasting name for each user. For a user with an ID of 1, the broadcasting name would be `web_notifications_1`.
218+
The `ActionCable.server.broadcast` call places a message in the Action Cable pubsub queue under a separate broadcasting name for each user. For a user with an ID of 1, the broadcasting name would be `web_notifications_1`.
219219
The channel has been instructed to stream everything that arrives at `web_notifications_1` directly to the client by invoking the
220220
`#received(data)` callback. The data is the hash sent as the second parameter to the server-side broadcast call, JSON encoded for the trip
221221
across the wire, and unpacked for the data argument arriving to `#received`.
@@ -234,7 +234,7 @@ class ChatChannel < ApplicationCable::Channel
234234
end
235235
```
236236

237-
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.
237+
If you pass an object as the first argument to `subscriptions.create`, that object will become the params hash in your cable channel. The keyword `channel` is required.
238238

239239
```coffeescript
240240
# Client-side, which assumes you've already requested the right to send web notifications
@@ -293,7 +293,7 @@ The rebroadcast will be received by all connected clients, _including_ the clien
293293

294294
### More complete examples
295295

296-
See the [rails/actioncable-examples](http://github.com/rails/actioncable-examples) repository for a full example of how to setup Action Cable in a Rails app and adding channels.
296+
See the [rails/actioncable-examples](http://github.com/rails/actioncable-examples) repository for a full example of how to setup Action Cable in a Rails app, and how to add channels.
297297

298298

299299
## Configuration
@@ -349,11 +349,11 @@ something like: `App.cable = ActionCable.createConsumer("/cable")`.
349349
The second option is to pass the server url through the `action_cable_meta_tag` in your layout.
350350
This uses a url or path typically set via `config.action_cable.url` in the environment configuration files, or defaults to "/cable".
351351

352-
This method is especially useful if your websocket url might change between environments. If you host your production server via https, you will need to use the wss scheme
352+
This method is especially useful if your WebSocket url might change between environments. If you host your production server via https, you will need to use the wss scheme
353353
for your ActionCable server, but development might remain http and use the ws scheme. You might use localhost in development and your
354354
domain in production.
355355

356-
In any case, to vary the websocket url between environments, add the following configuration to each environment:
356+
In any case, to vary the WebSocket url between environments, add the following configuration to each environment:
357357

358358
```ruby
359359
config.action_cable.url = "ws://example.com:28080"
@@ -440,7 +440,7 @@ The Ruby side of things is built on top of [websocket-driver](https://github.com
440440

441441
## Deployment
442442

443-
Action Cable is powered by a combination of websockets and threads. All of the
443+
Action Cable is powered by a combination of WebSockets and threads. All of the
444444
connection management is handled internally by utilizing Ruby’s native thread
445445
support, which means you can use all your regular Rails models with no problems
446446
as long as you haven’t committed any thread-safety sins.

actioncable/lib/action_cable/channel/base.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ module Channel
3232
#
3333
# == Action processing
3434
#
35-
# Unlike subclasses of ActionController::Base, channels do not follow a REST
36-
# constraint form for their actions. Instead, ActionCable operates through a
35+
# Unlike subclasses of ActionController::Base, channels do not follow a RESTful
36+
# constraint form for their actions. Instead, Action Cable operates through a
3737
# remote-procedure call model. You can declare any public method on the
3838
# channel (optionally taking a <tt>data</tt> argument), and this method is
3939
# automatically exposed as callable to the client.
@@ -63,10 +63,10 @@ module Channel
6363
# end
6464
# end
6565
#
66-
# In this example, subscribed/unsubscribed are not callable methods, as they
66+
# In this example, the subscribed and unsubscribed methods are not callable methods, as they
6767
# were already declared in ActionCable::Channel::Base, but <tt>#appear</tt>
6868
# and <tt>#away</tt> are. <tt>#generate_connection_token</tt> is also not
69-
# callable as it's a private method. You'll see that appear accepts a data
69+
# callable, since it's a private method. You'll see that appear accepts a data
7070
# parameter, which it then uses as part of its model call. <tt>#away</tt>
7171
# does not, since it's simply a trigger action.
7272
#
@@ -125,7 +125,7 @@ def action_methods
125125
protected
126126
# action_methods are cached and there is sometimes need to refresh
127127
# them. ::clear_action_methods! allows you to do that, so next time
128-
# you run action_methods, they will be recalculated
128+
# you run action_methods, they will be recalculated.
129129
def clear_action_methods!
130130
@action_methods = nil
131131
end
@@ -166,9 +166,9 @@ def perform_action(data)
166166
end
167167
end
168168

169-
# Called by the cable connection when its cut so the channel has a chance to cleanup with callbacks.
169+
# Called by the cable connection when its cut, so the channel has a chance to cleanup with callbacks.
170170
# This method is not intended to be called directly by the user. Instead, overwrite the #unsubscribed callback.
171-
def unsubscribe_from_channel
171+
def unsubscribe_from_channel # :nodoc:
172172
run_callbacks :unsubscribe do
173173
unsubscribed
174174
end
@@ -183,7 +183,7 @@ def subscribed
183183
end
184184

185185
# Called once a consumer has cut its cable connection. Can be used for cleaning up connections or marking
186-
# people as offline or the like.
186+
# users as offline or the like.
187187
def unsubscribed
188188
# Override in subclasses
189189
end
@@ -224,7 +224,6 @@ def delegate_connection_identifiers
224224
end
225225
end
226226

227-
228227
def subscribe_to_channel
229228
run_callbacks :subscribe do
230229
subscribed
@@ -237,7 +236,6 @@ def subscribe_to_channel
237236
end
238237
end
239238

240-
241239
def extract_action(data)
242240
(data['action'].presence || :receive).to_sym
243241
end

actioncable/lib/action_cable/channel/streams.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module ActionCable
22
module Channel
33
# Streams allow channels to route broadcastings to the subscriber. A broadcasting is, as discussed elsewhere, a pub/sub queue where any data
4-
# put into it is automatically sent to the clients that are connected at that time. It's purely an online queue, though. If you're not
5-
# streaming a broadcasting at the very moment it sends out an update, you'll not get that update when connecting later.
4+
# placed into it is automatically sent to the clients that are connected at that time. It's purely an online queue, though. If you're not
5+
# streaming a broadcasting at the very moment it sends out an update, you will not get that update, if you connect after it has been sent.
66
#
77
# Most commonly, the streamed broadcast is sent straight to the subscriber on the client-side. The channel just acts as a connector between
88
# the two parties (the broadcaster and the channel subscriber). Here's an example of a channel that allows subscribers to get all new
@@ -18,8 +18,10 @@ module Channel
1818
# end
1919
# end
2020
#
21-
# So the subscribers of this channel will get whatever data is put into the, let's say, `comments_for_45` broadcasting as soon as it's put there.
22-
# That looks like so from that side of things:
21+
# Based on the above example, the subscribers of this channel will get whatever data is put into the,
22+
# let's say, `comments_for_45` broadcasting as soon as it's put there.
23+
#
24+
# An example broadcasting for this channel looks like so:
2325
#
2426
# ActionCable.server.broadcast "comments_for_45", author: 'DHH', content: 'Rails is just swell'
2527
#
@@ -37,8 +39,8 @@ module Channel
3739
#
3840
# CommentsChannel.broadcast_to(@post, @comment)
3941
#
40-
# If you don't just want to parlay the broadcast unfiltered to the subscriber, you can supply a callback that lets you alter what goes out.
41-
# Example below shows how you can use this to provide performance introspection in the process:
42+
# If you don't just want to parlay the broadcast unfiltered to the subscriber, you can also supply a callback that lets you alter what is sent out.
43+
# The below example shows how you can use this to provide performance introspection in the process:
4244
#
4345
# class ChatChannel < ApplicationCable::Channel
4446
# def subscribed
@@ -70,7 +72,7 @@ module Streams
7072
# Start streaming from the named <tt>broadcasting</tt> pubsub queue. Optionally, you can pass a <tt>callback</tt> that'll be used
7173
# instead of the default of just transmitting the updates straight to the subscriber.
7274
def stream_from(broadcasting, callback = nil)
73-
# Hold off the confirmation until pubsub#subscribe is successful
75+
# Don't send the confirmation until pubsub#subscribe is successful
7476
defer_subscription_confirmation!
7577

7678
callback ||= default_stream_callback(broadcasting)

actioncable/lib/action_cable/connection/base.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
module ActionCable
44
module Connection
5-
# For every WebSocket the cable server is accepting, a Connection object will be instantiated. This instance becomes the parent
6-
# of all the channel subscriptions that are created from there on. Incoming messages are then routed to these channel subscriptions
7-
# based on an identifier sent by the cable consumer. The Connection itself does not deal with any specific application logic beyond
5+
# For every WebSocket the Action Cable server accepts, a Connection object will be instantiated. This instance becomes the parent
6+
# of all of the channel subscriptions that are created from there on. Incoming messages are then routed to these channel subscriptions
7+
# based on an identifier sent by the Action Cable consumer. The Connection itself does not deal with any specific application logic beyond
88
# authentication and authorization.
99
#
1010
# Here's a basic example:
@@ -33,8 +33,8 @@ module Connection
3333
# end
3434
# end
3535
#
36-
# First, we declare that this connection can be identified by its current_user. This allows us later to be able to find all connections
37-
# established for that current_user (and potentially disconnect them if the user was removed from an account). You can declare as many
36+
# First, we declare that this connection can be identified by its current_user. This allows us to later be able to find all connections
37+
# established for that current_user (and potentially disconnect them). You can declare as many
3838
# identification indexes as you like. Declaring an identification means that an attr_accessor is automatically set for that key.
3939
#
4040
# Second, we rely on the fact that the WebSocket connection is established with the cookies from the domain being sent along. This makes
@@ -65,8 +65,8 @@ def initialize(server, env)
6565
end
6666

6767
# Called by the server when a new WebSocket connection is established. This configures the callbacks intended for overwriting by the user.
68-
# This method should not be called directly. Rely on the #connect (and #disconnect) callback instead.
69-
def process
68+
# This method should not be called directly -- instead rely upon on the #connect (and #disconnect) callbacks.
69+
def process # :nodoc:
7070
logger.info started_request_message
7171

7272
if websocket.possible? && allow_request_origin?
@@ -76,7 +76,7 @@ def process
7676
end
7777
end
7878

79-
# Data received over the cable is handled by this method. It's expected that everything inbound is JSON encoded.
79+
# Data received over the WebSocket connection is handled by this method. It's expected that everything inbound is JSON encoded.
8080
# The data is routed to the proper channel that the connection has subscribed to.
8181
def receive(data_in_json)
8282
if websocket.alive?
@@ -88,7 +88,7 @@ def receive(data_in_json)
8888

8989
# Send raw data straight back down the WebSocket. This is not intended to be called directly. Use the #transmit available on the
9090
# Channel instead, as that'll automatically address the correct subscriber and wrap the message in JSON.
91-
def transmit(data)
91+
def transmit(data) # :nodoc:
9292
websocket.transmit data
9393
end
9494

actioncable/lib/action_cable/connection/identification.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Identification
1212

1313
class_methods do
1414
# Mark a key as being a connection identifier index that can then be used to find the specific connection again later.
15-
# Common identifiers are current_user and current_account, but could be anything really.
15+
# Common identifiers are current_user and current_account, but could be anything, really.
1616
#
1717
# Note that anything marked as an identifier will automatically create a delegate by the same name on any
1818
# channel instances created off the connection.

actioncable/lib/action_cable/connection/message_buffer.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module ActionCable
22
module Connection
3-
# Allows us to buffer messages received from the WebSocket before the Connection has been fully initialized and is ready to receive them.
4-
# Entirely internal operation and should not be used directly by the user.
5-
class MessageBuffer
3+
# Allows us to buffer messages received from the WebSocket before the Connection has been fully initialized, and is ready to receive them.
4+
class MessageBuffer # :nodoc:
65
def initialize(connection)
76
@connection = connection
87
@buffered_messages = []

actioncable/lib/action_cable/connection/subscriptions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
module ActionCable
44
module Connection
55
# Collection class for all the channel subscriptions established on a given connection. Responsible for routing incoming commands that arrive on
6-
# the connection to the proper channel. Should not be used directly by the user.
7-
class Subscriptions
6+
# the connection to the proper channel.
7+
class Subscriptions # :nodoc:
88
def initialize(connection)
99
@connection = connection
1010
@subscriptions = {}

actioncable/lib/action_cable/helpers/action_cable_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module ActionCableHelper
99
# <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
1010
# </head>
1111
#
12-
# This is then used by ActionCable to determine the url of your websocket server.
12+
# This is then used by Action Cable to determine the url of your WebSocket server.
1313
# Your CoffeeScript can then connect to the server without needing to specify the
1414
# url directly:
1515
#

actioncable/lib/action_cable/remote_connections.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module ActionCable
1313
# ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect
1414
#
1515
# This will disconnect all the connections established for
16-
# <tt>User.find(1)</tt> across all servers running on all machines, because
17-
# it uses the internal channel that all these servers are subscribed to.
16+
# <tt>User.find(1)</tt>, across all servers running on all machines, because
17+
# it uses the internal channel that all of these servers are subscribed to.
1818
class RemoteConnections
1919
attr_reader :server
2020

0 commit comments

Comments
 (0)