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

Add support for custom payloads and channels in broadcasting #54099

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
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'],
];
}
}