Skip to content

Commit 080f9b4

Browse files
committed
Document Action Cable Connection Callbacks
1 parent b66df44 commit 080f9b4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

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: 23 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

0 commit comments

Comments
 (0)