C name the channel in subscribe/unsubscribe :response frames

The protocol "<event>: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 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-04 12:30:27 +01:00
parent 88ed045899
commit 95d6af38cc
2 changed files with 39 additions and 2 deletions

View File

@ -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;
}

View File

@ -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']);
}
// =========================================================================