Skip to content

Conversation

@bensheldon
Copy link
Contributor

@bensheldon bensheldon commented Oct 3, 2025

Motivation / Background

Action Cable channels have a success_callback that is used in the implementation of stream_from and stream_to, which is used to ensure that streams subscriptions are established before the channel subscription confirmation is sent to the client.

👉🏻 This changes stream_from and stream_to to expose their own success_callback too. This callback can be used in Channels to defer behavior until the stream subscription is established.

For example, a channel might want to ensure there aren't any missed updates between when a record is fetched and rendered for the frontend, and when an Action Cable stream subscription is ultimately established:

class ReliableChannel < ActionCable::Base include Turbo::Streams::ActionHelper include ActionView::RecordIdentifier def subscribed resource = GlobalId::Locator.locate_signed params[:resource] rendered_at = Time.parse params[:rendered_at] # 👇 👇 👇 👇 👇 👇 stream_for(resource, success_callback: (lambda do resource.reload if resource.updated_at > rendered_at transmit turbo_stream_action_tag( :update, target: dom_id(resource), template: ApplicationController.render(resource, format: :html) ) end end) end end

Additional information

I think this is conceptually a good change because it better aligns Stream subscription lifecycle options with Channel subscription lifecycle options.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.
@rails-bot rails-bot bot added the actioncable label Oct 3, 2025
Comment on lines +115 to +119
:subscribed_method_end,
:after_subscribe_callback,
:room_1_success,
:room_2_success,
:transmit_subscription_confirmation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test surfaces the after_subscribe callback order happening before subscription confirmation. It doesn't imply that's intended though (#25333). I'm planning to look at that next.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fixed in #55825

@bensheldon bensheldon force-pushed the stream-from-callback branch from 4fc2d1a to 5d4cfc3 Compare October 3, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant