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

[5.3] Allow Broadcasting a notification now instead on queue #16867

Merged
merged 4 commits into from
Dec 21, 2016
Merged

[5.3] Allow Broadcasting a notification now instead on queue #16867

merged 4 commits into from
Dec 21, 2016

Conversation

themsaid
Copy link
Member

@themsaid themsaid commented Dec 19, 2016

This is an attempt to allow broadcasting notifications immediately instead of doing it via queue, to achieve this you'll need to add the following method in your Notification class:

public function broadcastNow(){
    return true;
}

This will have the BroadcastNotificationCreated $connection set to sync so that the queue manager use it as the connection name.


Update

Added a new BroadcastMessage that can be returned from toBroadcast, this new class has a now() method that we use to update the connection name.

public function toBroadcast()
{
    return (new BroadcastMessage([...]))->now();
}

@@ -44,6 +51,10 @@ public function __construct($notifiable, $notification, $data)
$this->data = $data;
$this->notifiable = $notifiable;
$this->notification = $notification;

if (method_exists($this->notification, 'broadcastNow')) {
$this->connection = $this->notification->broadcastNow() ? 'sync' : null;
Copy link
Member

Choose a reason for hiding this comment

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

Why should we assume the sync connection is called "sync". We can name it whatever we want? It's only the driver which is "sync", not the connection.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we assume the sync driver is called sync in multiple areas in the framework already, for example: https://github.com/laravel/framework/blob/5.3/src/Illuminate/Broadcasting/BroadcastManager.php#L107

Copy link
Member

Choose a reason for hiding this comment

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

Looks like a bit of a bug tbh.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we need to re-think the queue config in 5.4, and have a default "queue queue", and a default "sync queue"?

Copy link
Member Author

Choose a reason for hiding this comment

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

That sounds like a good solution to me.

@JosephSilber
Copy link
Member

Is there no way we can use the ShouldQueue interface here instead?

@taylorotwell
Copy link
Member

I think it might be a little cleaner to have a BroadcastMessage(array)->now() construct.

@taylorotwell
Copy link
Member

In real usage I think it would look something like:

return new BroadcastMessage($this->toArray())->now();

@taylorotwell
Copy link
Member

And then the BroadcastChannel would properly handle that message type in getData.

@taylorotwell taylorotwell merged commit c92a722 into laravel:5.3 Dec 21, 2016
@taylorotwell
Copy link
Member

Tweaked this to just use Queueable trait.

@tillkruss
Copy link
Collaborator

This is probably worth mentioning in the docs.

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

5 participants