generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Simplify the Broadcasting API #17
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This looks closer to the Rails API. Attention: this is a breaking change.
Hi Tony, Do you plan to expand the class ChannelMessagesController
{
public function store(Channel $channel)
{
//...
if (request()->wantsTurboStream()) {
return response()->turboStream(
$message,
target: 'channel_messages', // optional, default to plural name of the model
partial: 'messages._message', // optional, defaults to {resource}._{name}
data: [/** Control the data */],
);
}
return redirect()->route('channels.show', $channel);
}
//...
} If it's in your plan, can you also add the Have a good day, |
Hey, @tortuetorche Yeah, I'm going to work on that next. This PR is only for the broadcasting part. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Changed
turbo-rails
gem API. Now theBroacasts
trait no longer automatically broadcasts all the changes. Models will have to specify either the$broadcasts
property as a boolean/array or$broadcastsTo
property as string/array or abroadcastsTo()
method that should return either models or broadcasting channel instancesRemoved
Once merged, this will be the new API for broadcasting:
Manually broadcasting:
Using the trait will no longer broadcast model changes. Now, users can manually trigger the broadcasts whenever/wherever they want to, like so:
They can do that in a controller (after the resource is created or update, for instance) or in model events, like so:
This will broadcast the new message whenever one is created. This way gives you way more control over the broadcasts.
Automatically Broadcasting
In some cases, you may want to broadcast all changes happening to a model. You may achieve so by specifying adding a
$broadcasts = true
property to the model:This will broadcast Turbo Streams whenever the model is created, updated, or deleted. It will happen via background jobs, so you don't have to wait for the messages to be sent to your WebSocket service.
By default, this will send new instances of models using the
append
action. If you want to control which action it should use, you may specify the property as an array and tell in aninsertsBy
key which action it should use:This will still send all the model changes (created, updated, and deleted), and new entries will use the
prepend
action.Using only the
$broadcasts
property will send the Turbo Stream broadcast messages to this model's channel. You may want to send the Turbo Stream messages to related models or even to custom broadcasting channels. You may achieve so with either the$broadcastsTo
property or abroadcastsTo()
method, such as:You may combine the
$broadcasts = ['insertsBy' => 'prepend']
and thebroadcastsTo
to control how you want the new entries to be delivered to the broadcasting channels.TODO
issue #15
This will be a breaking change.