Skip to content

Commit

Permalink
Add support for custom payloads and channels in broadcasting (#54099)
Browse files Browse the repository at this point in the history
* Adding possibility to add payloads and channels specified for broadcasting channels

* Removing unused test function

* Removing typo

* formatting

* formatting

* remove types

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
JanneDeVos and taylorotwell authored Jan 9, 2025
1 parent 0a0acd9 commit ab7cdf6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/Illuminate/Broadcasting/BroadcastEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public function handle(BroadcastingFactory $manager)

foreach ($connections as $connection) {
$manager->connection($connection)->broadcast(
$channels, $name, $payload
$this->getConnectionChannels($channels, $connection),
$name,
$this->getConnectionPayload($payload, $connection)
);
}
}
Expand Down Expand Up @@ -134,6 +136,40 @@ protected function formatProperty($value)
return $value;
}

/**
* Get the channels for the given connection.
*
* @param array $channels
* @param string $connection
* @return array
*/
protected function getConnectionChannels($channels, $connection)
{
return is_array($channels[$connection] ?? null)
? $channels[$connection]
: $channels;
}

/**
* Get the payload for the given connection.
*
* @param array $payload
* @param string $connection
* @return array
*/
protected function getConnectionPayload($payload, $connection)
{
$connectionPayload = is_array($payload[$connection] ?? null)
? $payload[$connection]
: $payload;

if (isset($payload['socket'])) {
$connectionPayload['socket'] = $payload['socket'];
}

return $connectionPayload;
}

/**
* Get the display name for the queued job.
*
Expand Down
55 changes: 55 additions & 0 deletions tests/Broadcasting/BroadcastEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ public function testSpecificBroadcasterGiven()

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

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

$broadcaster->shouldReceive('broadcast')->once()->with(
['first-channel'], TestBroadcastEventWithChannelsPerConnection::class, ['firstName' => 'Taylor', 'lastName' => 'Otwell', 'collection' => ['foo' => 'bar']]
);

$broadcaster->shouldReceive('broadcast')->once()->with(
['second-channel'], TestBroadcastEventWithChannelsPerConnection::class, ['firstName' => 'Taylor']
);

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

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

$event = new TestBroadcastEventWithChannelsPerConnection;

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

class TestBroadcastEvent
Expand Down Expand Up @@ -101,3 +123,36 @@ public function __construct()
$this->broadcastVia('log');
}
}

class TestBroadcastEventWithChannelsPerConnection extends TestBroadcastEvent
{
public function broadcastConnections()
{
return [
'first_connection',
'second_connection',
];
}

public function broadcastWith()
{
return [
'first_connection' => [
'firstName' => 'Taylor',
'lastName' => 'Otwell',
'collection' => ['foo' => 'bar'],
],
'second_connection' => [
'firstName' => 'Taylor',
],
];
}

public function broadcastOn()
{
return [
'first_connection' => ['first-channel'],
'second_connection' => ['second-channel'],
];
}
}

0 comments on commit ab7cdf6

Please sign in to comment.