Skip to content

Commit

Permalink
[1.x] Allows for disconnections when broadcasting to others (#122)
Browse files Browse the repository at this point in the history
* probably fix toOthers() broadcast message

* fix-issue-121: Check that has any connections with this socket_id

* handle disconnected socket id

* wip

* wip

* fix tests

---------

Co-authored-by: Ivan Savichev <[email protected]>
Co-authored-by: Joe Dixon <[email protected]>
  • Loading branch information
3 people authored Apr 4, 2024
1 parent 9682e87 commit 0b5e383
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 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,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'])) {
Expand Down
22 changes: 22 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,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));
})
Expand Down

0 comments on commit 0b5e383

Please sign in to comment.