Skip to content

Commit ab7cdf6

Browse files
Add support for custom payloads and channels in broadcasting (#54099)
* 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]>
1 parent 0a0acd9 commit ab7cdf6

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

src/Illuminate/Broadcasting/BroadcastEvent.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ public function handle(BroadcastingFactory $manager)
9090

9191
foreach ($connections as $connection) {
9292
$manager->connection($connection)->broadcast(
93-
$channels, $name, $payload
93+
$this->getConnectionChannels($channels, $connection),
94+
$name,
95+
$this->getConnectionPayload($payload, $connection)
9496
);
9597
}
9698
}
@@ -134,6 +136,40 @@ protected function formatProperty($value)
134136
return $value;
135137
}
136138

139+
/**
140+
* Get the channels for the given connection.
141+
*
142+
* @param array $channels
143+
* @param string $connection
144+
* @return array
145+
*/
146+
protected function getConnectionChannels($channels, $connection)
147+
{
148+
return is_array($channels[$connection] ?? null)
149+
? $channels[$connection]
150+
: $channels;
151+
}
152+
153+
/**
154+
* Get the payload for the given connection.
155+
*
156+
* @param array $payload
157+
* @param string $connection
158+
* @return array
159+
*/
160+
protected function getConnectionPayload($payload, $connection)
161+
{
162+
$connectionPayload = is_array($payload[$connection] ?? null)
163+
? $payload[$connection]
164+
: $payload;
165+
166+
if (isset($payload['socket'])) {
167+
$connectionPayload['socket'] = $payload['socket'];
168+
}
169+
170+
return $connectionPayload;
171+
}
172+
137173
/**
138174
* Get the display name for the queued job.
139175
*

tests/Broadcasting/BroadcastEventTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ public function testSpecificBroadcasterGiven()
6464

6565
(new BroadcastEvent($event))->handle($manager);
6666
}
67+
68+
public function testSpecificChannelsPerConnection()
69+
{
70+
$broadcaster = m::mock(Broadcaster::class);
71+
72+
$broadcaster->shouldReceive('broadcast')->once()->with(
73+
['first-channel'], TestBroadcastEventWithChannelsPerConnection::class, ['firstName' => 'Taylor', 'lastName' => 'Otwell', 'collection' => ['foo' => 'bar']]
74+
);
75+
76+
$broadcaster->shouldReceive('broadcast')->once()->with(
77+
['second-channel'], TestBroadcastEventWithChannelsPerConnection::class, ['firstName' => 'Taylor']
78+
);
79+
80+
$manager = m::mock(BroadcastingFactory::class);
81+
82+
$manager->shouldReceive('connection')->once()->with('first_connection')->andReturn($broadcaster);
83+
$manager->shouldReceive('connection')->once()->with('second_connection')->andReturn($broadcaster);
84+
85+
$event = new TestBroadcastEventWithChannelsPerConnection;
86+
87+
(new BroadcastEvent($event))->handle($manager);
88+
}
6789
}
6890

6991
class TestBroadcastEvent
@@ -101,3 +123,36 @@ public function __construct()
101123
$this->broadcastVia('log');
102124
}
103125
}
126+
127+
class TestBroadcastEventWithChannelsPerConnection extends TestBroadcastEvent
128+
{
129+
public function broadcastConnections()
130+
{
131+
return [
132+
'first_connection',
133+
'second_connection',
134+
];
135+
}
136+
137+
public function broadcastWith()
138+
{
139+
return [
140+
'first_connection' => [
141+
'firstName' => 'Taylor',
142+
'lastName' => 'Otwell',
143+
'collection' => ['foo' => 'bar'],
144+
],
145+
'second_connection' => [
146+
'firstName' => 'Taylor',
147+
],
148+
];
149+
}
150+
151+
public function broadcastOn()
152+
{
153+
return [
154+
'first_connection' => ['first-channel'],
155+
'second_connection' => ['second-channel'],
156+
];
157+
}
158+
}

0 commit comments

Comments
 (0)