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

Introduce TurboPower::Broadcasts and TurboPower::Broadcastable modules #30

Merged
merged 2 commits into from
Oct 15, 2023

Conversation

marcoroth
Copy link
Owner

@marcoroth marcoroth commented Mar 10, 2023

This pull requests introduces the TurboPower::Broadcasts and TurboPower::Broadcastable modules which can be used to broadcast any TurboPower custom action in jobs, controllers, models or whereever you can run Ruby code within your app.

The TurboPower::Broadcasts module provides the broadcast actions in synchronous and asynchronous form for the Turbo::StreamsChannel, meaning an action like console_log can be used like:

Turbo::StreamsChannel.broadcast_console_log_to(:posts, message: "Hello World", level: :info)
Turbo::StreamsChannel.broadcast_console_log_later_to(:posts, message: "Hello World", level: :info)

Which would broadcast the console_log action to any client which is subscribed via:

<%= turbo_stream_from :posts %>

The TurboPower::Broadcastable module allows actions to be broadcasted from ActiveRecord models, like:

# app/models/post.rb

class Post < ApplicationRecord
  after_save do
    # broadcasting to all clients which are subscribed via:
    # <%= turbo_stream_from @post %>
    broadcast_console_log(message: "Hello World", level: "Warn")
    broadcast_console_log_later(message: "Hello World", level: "Warn")

    # broadcasting to all clients which are subscribed via the `:posts` stream identifier:  
    # <%= turbo_stream_from :posts %>
    broadcast_console_log_to(:posts, message: "Hello World", level: "Warn")
    broadcast_console_log_later_to(:posts, message: "Hello World", level: "Warn")
  end
end

@marcoroth marcoroth force-pushed the turbo-power-broadcasts branch from bddb17c to 72c0b47 Compare March 10, 2023 18:04
@pedraal
Copy link

pedraal commented Oct 8, 2023

Hi @marcoroth , first of all thanks for your job on this great repo ! Any plan on merging this branch someday ?

@marcoroth
Copy link
Owner Author

Hey @pedraal, yes! It's going to happen very soon actually. I wanted to get it merged for Rails World but I didn't manage to. But this is next up

@marcoroth marcoroth merged commit 96db5be into main Oct 15, 2023
@marcoroth marcoroth deleted the turbo-power-broadcasts branch October 15, 2023 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants