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

[11.x] Adds anonymous broadcasting #51082

Merged
merged 9 commits into from
Apr 16, 2024
Merged

Conversation

joedixon
Copy link
Contributor

@joedixon joedixon commented Apr 16, 2024

Sometimes you may wish to broadcast an ad-hoc event.

An ad-hoc event is one where you don't need to hook into it anywhere else in your application. You just want to notify the frontend of something.

For this, you don't want to go to the trouble of creating a brand new event, you just want to fire off a message.

For this, we can use an anonymous broadcast using the Broadcast facade, which can be as simple as:

Broadcast::on('my-channel')->send();

Results in...

{
    "event":"AnonymousBroadcastable",
    "data":"[]",
    "channel":"my-channel"
}

You may dispatch to multiple channels at the same time:

Broadcast::on(['my-channel', new PrivateChannel('my-channel'), 'presence-my-channel')->send();

You may also configure the name, payload, broadcasting connections and whether or not it should only be dispatched to others:

Broadcast::on('my-channel')
    ->as('my-event-name')
    ->with(['some' => 'array', 'of' => 'data'])
    ->via('reverb')
    ->toOthers()
    ->send();

Results in...

{
    "event":"my-event-name",
    "data":"{\"some\":\"array\",\"of\":\"data\"}",
    "channel":"test-channel"
}

Edit:

After feedback from @lukeraymonddowning, the following conveniences were added:

Broadcast::private('my-channel')->send();

Broadcast::presence('my-channel')->send();

@lukeraymonddowning
Copy link
Contributor

Would it be worth having a Broadcast::private method as a little convenience helper?

Broadcast::private('my-channel')
    ->as('my-event-name')
    ->with(['some' => 'array', 'of' => 'data'])
    ->via('reverb')
    ->toOthers()
    ->send();

@joedixon joedixon force-pushed the feat/anonymous-broadcastable branch from 74d7b54 to 5264f43 Compare April 16, 2024 14:14
@joedixon
Copy link
Contributor Author

Would it be worth having a Broadcast::private method as a little convenience helper?

Broadcast::private('my-channel')
    ->as('my-event-name')
    ->with(['some' => 'array', 'of' => 'data'])
    ->via('reverb')
    ->toOthers()
    ->send();

Consider it done 🚀

@devajmeireles
Copy link
Contributor

Awesome! Will be a very good addition. I'd like to suggest an interesting addition in my opinion.

As we know Broadcast has two ways of being dispatched, ShouldBroadcast and ShouldBroadcastNow.

From what I can understand, this will always be dispatched using ShouldBroadcast, which requires queues.

So, it might be interesting to think about something like:

Broadcast::on('my-channel')->send();
Broadcast::on('my-channel')->sendNow();

Or simply:

Broadcast::on('my-channel')->send(queueable: false);

An easy way to achieve this result would be to change the implementation of send by making it consume two possible Broadcast classes. One implementing ShouldBroadcast and the other implementing ShouldBroadcastNow.

@taylorotwell
Copy link
Member

@devajmeireles implemented.

@taylorotwell taylorotwell merged commit 637bd31 into 11.x Apr 16, 2024
29 checks passed
@taylorotwell taylorotwell deleted the feat/anonymous-broadcastable branch April 16, 2024 18:32
@antonkomarev
Copy link
Contributor

@joedixon Cool feature. But is there is a way to broadcast after db commit as we can do with events by implementing interface ShouldDispatchAfterCommit?

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.

None yet

6 participants