Add Action Cable command callbacks at the connection level #44696
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for command callbacks to
ActionCable::Connection::Base
. For example,the
before_command
callback is invoked before a command is processed by a channel. Theafter_command
andaround_command
callbacks work similarly.Other Information
The main motivation behind this PR is to make working with multi-tenant apps easier. Most multi-tenancy libs (apartment, acts_as_tenant to name a few) provide an API to set the current tenant for the block. To make sure that channels code is executed within a particular context, we had to either add smth like
Tenant.switch! tenant
before every action (too error-prone) or patching the#dispatch_websocket_message
method (better, but hacky):I believe, we need an official API for that (and other similar use cases). With the proposed API, the example above could be rewritten as: