Skip to content

Commit

Permalink
handle disconnected socket id
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Apr 4, 2024
1 parent a85508f commit 88858c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Protocols/Pusher/Http/Controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,8 +45,7 @@ public function __invoke(RequestInterface $request, Connection $connection, stri
'channels' => $channels,
'data' => $payload['data'],
],
isset($payload['socket_id']) ? (!empty($this->channels?->connections()[$payload['socket_id']]) ?
$this->channels?->connections()[$payload['socket_id']]->connection() : null) : null
$except ? $except->connection() : null
);

if (isset($payload['info'])) {
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/Protocols/Pusher/Reverb/EventsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@
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' => 'data']),
'socket_id' => 'invalid-socket-id',
]));

$connection->assertReceived('{"event":"NewEvent","data":"{\"some\":\"data\"}","channel":"test-channel-two"}', 2);
expect($response->getStatusCode())->toBe(200);
expect($response->getBody()->getContents())->toBe('{}');
});

it('validates invalid data', function ($payload) {
await($this->signedPostRequest('events', $payload));
})
Expand Down

0 comments on commit 88858c2

Please sign in to comment.