Skip to content

Commit

Permalink
add forks
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 29, 2023
1 parent 521d932 commit db5af15
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"illuminate/support": "^10.0",
"ratchet/rfc6455": "^0.3.1",
"react/socket": "^1.14",
"spatie/fork": "^1.2",
"symfony/http-foundation": "^6.3"
},
"require-dev": {
Expand Down
15 changes: 12 additions & 3 deletions src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laravel\Reverb\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Contracts\Connection;
use Spatie\Fork\Fork;

class Channel
{
Expand Down Expand Up @@ -104,9 +105,17 @@ public function broadcastToAll(array $payload): void
{
$message = json_encode($payload);

foreach ($this->connections() as $connection) {
$connection->send($message);
}
$chunks = array_chunk($this->connections(), 5000);

$forks = array_map(function ($connections) use ($message) {
return function () use ($connections, $message) {
foreach ($connections as $connection) {
$connection->send($message);
}
};
}, $chunks);

(new Fork)->run(...$forks);
}

/**
Expand Down

0 comments on commit db5af15

Please sign in to comment.