|
4 | 4 |
|
5 | 5 | use Illuminate\Broadcasting\BroadcastException;
|
6 | 6 | use Illuminate\Contracts\Redis\Factory as Redis;
|
| 7 | +use Illuminate\Redis\Connections\PhpRedisClusterConnection; |
| 8 | +use Illuminate\Redis\Connections\PredisClusterConnection; |
| 9 | +use Illuminate\Redis\Connections\PredisConnection; |
7 | 10 | use Illuminate\Support\Arr;
|
| 11 | +use Predis\Connection\Cluster\RedisCluster; |
8 | 12 | use Predis\Connection\ConnectionException;
|
9 | 13 | use RedisException;
|
10 | 14 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
@@ -125,10 +129,30 @@ public function broadcast(array $channels, $event, array $payload = [])
|
125 | 129 | ]);
|
126 | 130 |
|
127 | 131 | try {
|
128 |
| - $connection->eval( |
129 |
| - $this->broadcastMultipleChannelsScript(), |
130 |
| - 0, $payload, ...$this->formatChannels($channels) |
131 |
| - ); |
| 132 | + if ($connection instanceof PhpRedisClusterConnection) { |
| 133 | + foreach ($channels as $channel) { |
| 134 | + $connection->publish($channel, $payload); |
| 135 | + } |
| 136 | + } elseif ($connection instanceof PredisClusterConnection && |
| 137 | + $connection->client()->getConnection() instanceof RedisCluster) { |
| 138 | + $randomClusterNodeConnection = new PredisConnection( |
| 139 | + $connection->client()->getClientBy('slot', mt_rand(0, 16383)) |
| 140 | + ); |
| 141 | + |
| 142 | + if ($events = $connection->getEventDispatcher()) { |
| 143 | + $randomClusterNodeConnection->setEventDispatcher($events); |
| 144 | + } |
| 145 | + |
| 146 | + $randomClusterNodeConnection->eval( |
| 147 | + $this->broadcastMultipleChannelsScript(), |
| 148 | + 0, $payload, ...$this->formatChannels($channels) |
| 149 | + ); |
| 150 | + } else { |
| 151 | + $connection->eval( |
| 152 | + $this->broadcastMultipleChannelsScript(), |
| 153 | + 0, $payload, ...$this->formatChannels($channels) |
| 154 | + ); |
| 155 | + } |
132 | 156 | } catch (ConnectionException|RedisException $e) {
|
133 | 157 | throw new BroadcastException(
|
134 | 158 | sprintf('Redis error: %s.', $e->getMessage())
|
|
0 commit comments