Skip to content

Commit

Permalink
[1.x] Allow publishing to others only when using Redis (#275)
Browse files Browse the repository at this point in the history
* Fix for issue #246

Pass SocketID through redis, and filter it on the receiving end.

This ensures that ->toOthers() works with scaling = true

* update tests

* formatting

* wip

* Update PusherPubSubIncomingMessageHandler.php

---------

Co-authored-by: Joe Dixon <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Nov 27, 2024
1 parent 05ec48d commit c312511
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Protocols/Pusher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function dispatch(Application $app, array $payload, ?Connection $c
'type' => 'message',
'application' => serialize($app),
'payload' => $payload,
'socket_id' => $connection?->id(),
]);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Protocols/Pusher/PusherPubSubIncomingMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ public function handle(string $payload): void

$application = unserialize($event['application']);

$except = isset($event['socket_id']) ?
app(ChannelManager::class)->for($application)->connections()[$event['socket_id']] ?? null
: null;

match ($event['type'] ?? null) {
'message' => EventDispatcher::dispatchSynchronously(
$application,
$event['payload']
$event['payload'],
$except?->connection()
),
'metrics' => app(MetricsHandler::class)->publish(
$application,
Expand Down
23 changes: 23 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,29 @@
expect($response->getBody()->getContents())->toBe('{}');
});

it('can ignore a subscriber when publishing events over redis', function () {
$this->usingRedis();

$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' => $connection->socketId(),
]));

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

it('does not fail when ignoring an invalid subscriber', function () {
$connection = connect();
subscribe('test-channel-two', connection: $connection);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
app(ServerProviderManager::class)->withPublishing();
$pubSub = Mockery::mock(PubSubProvider::class);
$pubSub->shouldReceive('publish')->once()
->with(['type' => 'message', 'application' => serialize($app), 'payload' => ['channel' => 'test-channel']]);
->with(['type' => 'message', 'application' => serialize($app), 'payload' => ['channel' => 'test-channel'], 'socket_id' => null]);

$this->app->instance(PubSubProvider::class, $pubSub);

Expand Down

0 comments on commit c312511

Please sign in to comment.