Skip to content

[8.x] Ability to specify the broadcaster to use when broadcasting an event #38086

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

Merged
merged 5 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 14 additions & 7 deletions src/Illuminate/Broadcasting/BroadcastEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Illuminate\Broadcasting;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\Broadcaster;
use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -52,10 +52,10 @@ public function __construct($event)
/**
* Handle the queued job.
*
* @param \Illuminate\Contracts\Broadcasting\Broadcaster $broadcaster
* @param \Illuminate\Contracts\Broadcasting\Factory $manager
* @return void
*/
public function handle(Broadcaster $broadcaster)
public function handle(BroadcastingFactory $manager)
{
$name = method_exists($this->event, 'broadcastAs')
? $this->event->broadcastAs() : get_class($this->event);
Expand All @@ -66,10 +66,17 @@ public function handle(Broadcaster $broadcaster)
return;
}

$broadcaster->broadcast(
$channels, $name,
$this->getPayloadFromEvent($this->event)
);
$connections = method_exists($this->event, 'broadcastConnections')
? $this->event->broadcastConnections()
: [null];

$payload = $this->getPayloadFromEvent($this->event);

foreach ($connections as $connection) {
$manager->connection($connection)->broadcast(
$channels, $name, $payload
);
}
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/Illuminate/Broadcasting/InteractsWithBroadcasting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Broadcasting;

use Illuminate\Support\Arr;

trait InteractsWithBroadcasting
{
/**
* The broadcaster connection to use to broadcast the event.
*
* @var array
*/
protected $broadcastConnection = [null];

/**
* Broadcast the event using a specific broadcaster.
*
* @param array|string|null $connection
* @return $this
*/
public function broadcastVia($connection = null)
{
$this->broadcastConnection = is_null($connection)
? [null]
: Arr::wrap($connection);

return $this;
}

/**
* Get the broadcaster connections the event should be broadcast on.
*
* @return array
*/
public function broadcastConnections()
{
return $this->broadcastConnection;
}
}
15 changes: 15 additions & 0 deletions src/Illuminate/Broadcasting/PendingBroadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ public function __construct(Dispatcher $events, $event)
$this->events = $events;
}

/**
* Broadcast the event using a specific broadcaster.
*
* @param string|null $connection
* @return $this
*/
public function via($connection = null)
{
if (method_exists($this->event, 'broadcastVia')) {
$this->event->broadcastVia($connection);
}

return $this;
}

/**
* Broadcast the event to everyone except the current user.
*
Expand Down
39 changes: 37 additions & 2 deletions tests/Broadcasting/BroadcastEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Illuminate\Tests\Broadcasting;

use Illuminate\Broadcasting\BroadcastEvent;
use Illuminate\Broadcasting\InteractsWithBroadcasting;
use Illuminate\Contracts\Broadcasting\Broadcaster;
use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand All @@ -22,9 +24,13 @@ public function testBasicEventBroadcastParameterFormatting()
['test-channel'], TestBroadcastEvent::class, ['firstName' => 'Taylor', 'lastName' => 'Otwell', 'collection' => ['foo' => 'bar']]
);

$manager = m::mock(BroadcastingFactory::class);

$manager->shouldReceive('connection')->once()->with(null)->andReturn($broadcaster);

$event = new TestBroadcastEvent;

(new BroadcastEvent($event))->handle($broadcaster);
(new BroadcastEvent($event))->handle($manager);
}

public function testManualParameterSpecification()
Expand All @@ -35,9 +41,28 @@ public function testManualParameterSpecification()
['test-channel'], TestBroadcastEventWithManualData::class, ['name' => 'Taylor', 'socket' => null]
);

$manager = m::mock(BroadcastingFactory::class);

$manager->shouldReceive('connection')->once()->with(null)->andReturn($broadcaster);

$event = new TestBroadcastEventWithManualData;

(new BroadcastEvent($event))->handle($broadcaster);
(new BroadcastEvent($event))->handle($manager);
}

public function testSpecificBroadcasterGiven()
{
$broadcaster = m::mock(Broadcaster::class);

$broadcaster->shouldReceive('broadcast')->once();

$manager = m::mock(BroadcastingFactory::class);

$manager->shouldReceive('connection')->once()->with('log')->andReturn($broadcaster);

$event = new TestBroadcastEventWithSpecificBroadcaster;

(new BroadcastEvent($event))->handle($manager);
}
}

Expand Down Expand Up @@ -66,3 +91,13 @@ public function broadcastWith()
return ['name' => 'Taylor'];
}
}

class TestBroadcastEventWithSpecificBroadcaster extends TestBroadcastEvent
{
use InteractsWithBroadcasting;

public function __construct()
{
$this->broadcastVia('log');
}
}