Repository: laravel/reverb (php)

Issue:
Broadcast message toOthers throws ApiException in pusher
### Reverb Version

1.0.0-beta4

### Laravel Version

11.1.0

### PHP Version

8.3.4

### Description

When i try to send broadcast message via my Event like this
`broadcast(new MyEvent())->toOthers()`
I receive an Exception
```
[2024-03-28 16:26:08] local.ERROR: Pusher error: Internal server error.. {"exception":"[object] (Illuminate\\Broadcasting\\BroadcastException(code: 0): Pusher error: Internal server error.. at /home/ss/workspace/chat/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php:164)
[stacktrace]
#0 /home/ss/workspace/chat/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php(92): Illuminate\\Broadcasting\\Broadcasters\\PusherBroadcaster->broadcast()
```
In laravel-websockets, which i used before, same method work without errors.

### Steps To Reproduce

`broadcast(new MyEvent())->toOthers()`
receive exception
```
[2024-03-28 16:26:08] local.ERROR: Pusher error: Internal server error.. {"exception":"[object] (Illuminate\\Broadcasting\\BroadcastException(code: 0): Pusher error: Internal server error.. at /home/ss/workspace/chat/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php:164)
[stacktrace]
#0 /home/ss/workspace/chat/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php(92): Illuminate\\Broadcasting\\Broadcasters\\PusherBroadcaster->broadcast()
```
When i add in Reverb inside handleRequest method of Laravel\Reverb\Servers\Reverb\Http\Server
`            $this->close($connection, 500, $e->getMessage());
`

instead of
`            $this->close($connection, 500, 'Internal server error.');
`
Which a give ZERO info about what happening inside this error
I receive some more info
```
message
: 
"Pusher error: Undefined array key \"875833828.561499442\"."
```
Maybe problem in line
`            isset($payload['socket_id']) ? $this->channels->connections()[$payload['socket_id']]->connection() : null
`
But didn't understand what is wrong. When remove ->toOthers() - all work good
Thank you


Discussion:

Patch:
diff --git a/src/Protocols/Pusher/Http/Controllers/EventsController.php b/src/Protocols/Pusher/Http/Controllers/EventsController.php
--- a/src/Protocols/Pusher/Http/Controllers/EventsController.php
+++ b/src/Protocols/Pusher/Http/Controllers/EventsController.php
@@ -34,6 +34,9 @@ public function __invoke(RequestInterface $request, Connection $connection, stri
         }
 
         $channels = Arr::wrap($payload['channels'] ?? $payload['channel'] ?? []);
+        if ($except = $payload['socket_id'] ?? null) {
+            $except = $this->channels->connections()[$except] ?? null;
+        }
 
         EventDispatcher::dispatch(
             $this->application,
@@ -42,7 +45,7 @@ public function __invoke(RequestInterface $request, Connection $connection, stri
                 'channels' => $channels,
                 'data' => $payload['data'],
             ],
-            isset($payload['socket_id']) ? $this->channels->connections()[$payload['socket_id']]->connection() : null
+            $except ? $except->connection() : null
         );
 
         if (isset($payload['info'])) {


Test patch:
diff --git a/tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php b/tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php
--- a/tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php
+++ b/tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php
@@ -78,6 +78,28 @@
     expect($response->getBody()->getContents())->toBe('{}');
 });
 
+it('does not fail when ignoring an invalid subscriber', function () {
+    $connection = connect();
+    subscribe('test-channel-two', connection: $connection);
+    $response = await($this->signedPostRequest('events', [
+        'name' => 'NewEvent',
+        'channels' => ['test-channel-one', 'test-channel-two'],
+        'data' => json_encode(['some' => 'data']),
+    ]));
+
+    $response = await($this->signedPostRequest('events', [
+        'name' => 'NewEvent',
+        'channels' => ['test-channel-one', 'test-channel-two'],
+        'data' => json_encode(['some' => ['more' => 'data']]),
+        'socket_id' => 'invalid-socket-id',
+    ]));
+
+    $connection->assertReceived('{"event":"NewEvent","data":"{\"some\":\"data\"}","channel":"test-channel-two"}', 1);
+    $connection->assertReceived('{"event":"NewEvent","data":"{\"some\":{\"more\":\"data\"}}","channel":"test-channel-two"}', 1);
+    expect($response->getStatusCode())->toBe(200);
+    expect($response->getBody()->getContents())->toBe('{}');
+});
+
 it('validates invalid data', function ($payload) {
     await($this->signedPostRequest('events', $payload));
 })