Skip to content

Commit cf2be23

Browse files
authored
Merge pull request rails#45469 from skryukov/docs-for-action-cable-channel-callbacks [ci-skip]
Document Action Cable Callbacks
2 parents 1d769c6 + 080f9b4 commit cf2be23

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

actioncable/lib/action_cable/channel/callbacks.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44

55
module ActionCable
66
module Channel
7+
# = Action Cable Channel Callbacks
8+
#
9+
# Action Cable Channel provides hooks during the life cycle of a channel subscription.
10+
# Callbacks allow triggering logic during this cycle. Available callbacks are:
11+
#
12+
# * <tt>before_subscribe</tt>
13+
# * <tt>after_subscribe</tt> (also aliased as: <tt>on_subscribe</tt>)
14+
# * <tt>before_unsubscribe</tt>
15+
# * <tt>after_unsubscribe</tt> (also aliased as: <tt>on_unsubscribe</tt>)
16+
#
17+
# NOTE: the <tt>after_subscribe</tt> callback is triggered whenever
18+
# the <tt>subscribed</tt> method is called, even if subscription was rejected
19+
# with the <tt>reject</tt> method.
20+
# To trigger <tt>after_subscribe</tt> only on successful subscriptions,
21+
# use <tt>after_subscribe :my_method_name, unless: :subscription_rejected?</tt>
22+
#
723
module Callbacks
824
extend ActiveSupport::Concern
925
include ActiveSupport::Callbacks

actioncable/lib/action_cable/connection/callbacks.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@
44

55
module ActionCable
66
module Connection
7+
# = Action Cable Connection Callbacks
8+
#
9+
# There are <tt>before_command</tt>, <tt>after_command</tt>, and <tt>around_command</tt> callbacks
10+
# available to be invoked before, after or around every command received by a client respectively.
11+
# The term "command" here refers to any interaction received by a client (subscribing, unsubscribing or performing actions):
12+
#
13+
# module ApplicationCable
14+
# class Connection < ActionCable::Connection::Base
15+
# identified_by :user
16+
#
17+
# around_command :set_current_account
18+
#
19+
# private
20+
#
21+
# def set_current_account
22+
# # Now all channels could use Current.account
23+
# Current.set(account: user.account) { yield }
24+
# end
25+
# end
26+
# end
27+
#
728
module Callbacks
829
extend ActiveSupport::Concern
930
include ActiveSupport::Callbacks

guides/source/action_cable_overview.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ end
165165

166166
[`rescue_from`]: https://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#method-i-rescue_from
167167

168+
#### Connection Callbacks
169+
170+
There are `before_command`, `after_command`, and `around_command` callbacks available to be invoked before, after or around every command received by a client respectively.
171+
The term "command" here refers to any interaction received by a client (subscribing, unsubscribing or performing actions):
172+
173+
```ruby
174+
# app/channels/application_cable/connection.rb
175+
module ApplicationCable
176+
class Connection < ActionCable::Connection::Base
177+
identified_by :user
178+
179+
around_command :set_current_account
180+
181+
private
182+
183+
def set_current_account
184+
# Now all channels could use Current.account
185+
Current.set(account: user.account) { yield }
186+
end
187+
end
188+
end
189+
```
190+
168191
### Channels
169192

170193
A *channel* encapsulates a logical unit of work, similar to what a controller does in a
@@ -234,6 +257,38 @@ class ChatChannel < ApplicationCable::Channel
234257
end
235258
```
236259

260+
#### Channel Callbacks
261+
262+
`ApplicationCable::Channel` provides a number of callbacks that can be used to trigger logic
263+
during the life cycle of a channel. Available callbacks are:
264+
265+
- `before_subscribe`
266+
- `after_subscribe` (also aliased as: `on_subscribe`)
267+
- `before_unsubscribe`
268+
- `after_unsubscribe` (also aliased as: `on_unsubscribe`)
269+
270+
NOTE: The `after_subscribe` callback is triggered whenever the `subscribed` method is called,
271+
even if subscription was rejected with the `reject` method. To trigger `after_subscribe`
272+
only on successful subscriptions, use `after_subscribe :send_welcome_message, unless: :subscription_rejected?`
273+
274+
```ruby
275+
# app/channels/chat_channel.rb
276+
class ChatChannel < ApplicationCable::Channel
277+
after_subscribe :send_welcome_message, unless: :subscription_rejected?
278+
after_subscribe :track_subscription
279+
280+
private
281+
282+
def send_welcome_message
283+
broadcast_to(...)
284+
end
285+
286+
def track_subscription
287+
# ...
288+
end
289+
end
290+
```
291+
237292
## Client-Side Components
238293

239294
### Connections

0 commit comments

Comments
 (0)