From 95d6af38cc987a7b96438db69be1d8e4e5741686 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 4 Nov 2026 12:30:27 +0100 Subject: [PATCH] C name the channel in subscribe/unsubscribe :response frames The protocol ":response" Success frame carried no channel, while the ":error" reply to a refused subscribe does. A client with several subscribes in flight (all sent as websocket.subscribe) could not tell which one a reply belongs to, so a refused private subscribe rejected every pending subscribe, and another channel's Success could resolve it. handleProtocolEvent() now adds "channel" to the Success frame (from message.channel or data.channel). The field is additive: the networking client still correlates on the event name and ignores it until it learns to match on msg.channel. Co-Authored-By: Claude Opus 5.5 --- src/Websocket/Handler.php | 13 +++++++++++-- tests/HandlerLifecycleTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Websocket/Handler.php b/src/Websocket/Handler.php index 4a6ddb8..366adeb 100644 --- a/src/Websocket/Handler.php +++ b/src/Websocket/Handler.php @@ -504,12 +504,21 @@ class Handler implements MessageComponentInterface return false; } - $connection->send(json_encode([ + $response = [ 'event' => $event . ':response', 'data' => [ 'message' => 'Success', ], - ])); + ]; + + // Name the channel, as the :error reply to a refused subscribe does, + // so a client with several subscribes in flight can match the reply. + $channelName = $message['channel'] ?? ($message['data']['channel'] ?? null); + if (is_string($channelName) && $channelName !== '') { + $response['channel'] = $channelName; + } + + $connection->send(json_encode($response)); return true; } diff --git a/tests/HandlerLifecycleTest.php b/tests/HandlerLifecycleTest.php index 2159676..f3ebe8c 100644 --- a/tests/HandlerLifecycleTest.php +++ b/tests/HandlerLifecycleTest.php @@ -695,6 +695,33 @@ class HandlerLifecycleTest extends TestCase $response = collect($connection->sentData)->firstWhere('event', 'websocket.subscribe:response'); $this->assertNotNull($response); $this->assertEquals('Success', $response['data']['message']); + $this->assertSame('websocket', $response['channel']); + } + + public function test_responses_to_concurrent_subscribes_name_their_channel() + { + $connection = $this->newConnection(); + $this->wsHandler->onOpen($connection); + + $this->wsHandler->onMessage($connection, new Mocks\Message([ + 'event' => 'websocket.subscribe', + 'data' => ['channel' => 'websocket'], + ])); + $this->wsHandler->onMessage($connection, new Mocks\Message([ + 'event' => 'websocket.subscribe', + 'data' => ['auth' => 'TestKey:invalid-signature', 'channel' => 'private-test'], + ])); + + $replies = collect($connection->sentData) + ->filter(fn ($frame) => str_starts_with($frame['event'] ?? '', 'websocket.subscribe:')) + ->map(fn ($frame) => [$frame['event'], $frame['channel'] ?? null]) + ->values() + ->all(); + + $this->assertSame([ + ['websocket.subscribe:response', 'websocket'], + ['websocket.subscribe:error', 'private-test'], + ], $replies); } public function test_unsubscribe_gets_response_suffix() @@ -709,6 +736,7 @@ class HandlerLifecycleTest extends TestCase $response = collect($connection->sentData)->firstWhere('event', 'websocket.unsubscribe:response'); $this->assertNotNull($response); + $this->assertSame('websocket', $response['channel']); } // =========================================================================