diff --git a/composer.json b/composer.json index 2b825fbe..7c2208f7 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Channels/Channel.php b/src/Channels/Channel.php index 753fc44b..ea6d9a2a 100644 --- a/src/Channels/Channel.php +++ b/src/Channels/Channel.php @@ -4,6 +4,7 @@ use Laravel\Reverb\Contracts\ChannelConnectionManager; use Laravel\Reverb\Contracts\Connection; +use Spatie\Fork\Fork; class Channel { @@ -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); } /**