Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cable] Support the stream_for and broadcast_to methods #121

Open
rsamoilov opened this issue Jan 9, 2025 · 0 comments
Open

[Cable] Support the stream_for and broadcast_to methods #121

rsamoilov opened this issue Jan 9, 2025 · 0 comments
Labels
good first issue Good for newcomers

Comments

@rsamoilov
Copy link
Member

Description

Currently, Rage only supports creating streams using the stream_from method. Implementing stream_for would allow users to create streams based on the (channel, object) pair.

Example:

class CommentsChannel < Rage::Cable::Channel
  def subscribed
    user = User.find(params[:id])
    stream_for user
  end
end

The following code will create a stream with the name based on the name of the channel and the object identifier.

Later, broadcast_to can be used to post messages to this stream:

CommentsChannel.broadcast_to(user, { comment: { body: "" } })

How to build object identifiers

To automatically create a stream based on the (channel, object) pair, we need to generate an identifier for object. The process of creating object identifiers should be performant and consistent across different servers (which is why we can’t use object_id). Additionally, stream_for should accept any Ruby objects, not just ActiveRecord models.

Using #id for ActiveRecord models and #as_json.to_s for non-ActiveRecord objects should satisfy the requirements, but I’m also open to other suggestions.

Tips

@rsamoilov rsamoilov added the good first issue Good for newcomers label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant