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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class BroadcastNotificationCreated implements ShouldBroadcast
*/
public $notification;

/**
* The queue connection.
*
* @var string
*/
public $connection;

/**
* The notification data.
*
Expand All @@ -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.

}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/Notifications/NotificationBroadcastChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public function testNotificationIsBroadcastedOnCustomChannels()

$this->assertEquals(new PrivateChannel('custom-channel'), $channels[0]);
}

public function testNotificationIsBroadcastedNow()
{
$notification = new TestNotificationBroadCastedNow;
$notification->id = 1;
$notifiable = Mockery::mock();

$event = new Illuminate\Notifications\Events\BroadcastNotificationCreated(
$notifiable, $notification, $notification->toArray($notifiable)
);

$this->assertEquals('sync', $event->connection);
}
}

class NotificationBroadcastChannelTestNotification extends Notification
Expand All @@ -59,3 +72,16 @@ public function broadcastOn()
return [new PrivateChannel('custom-channel')];
}
}

class TestNotificationBroadCastedNow extends Notification
{
public function toArray($notifiable)
{
return ['invoice_id' => 1];
}

public function broadcastNow()
{
return true;
}
}