Skip to content

fix(server): SP3 — bind member to connection_id on WS join; rewrite broadcast routing#341

Merged
detain merged 1 commit into
masterfrom
fix/server-sp3-syncplay-member-binding
Jun 29, 2026
Merged

fix(server): SP3 — bind member to connection_id on WS join; rewrite broadcast routing#341
detain merged 1 commit into
masterfrom
fix/server-sp3-syncplay-member-binding

Conversation

@detain

@detain detain commented Jun 29, 2026

Copy link
Copy Markdown
Owner

Summary

Implements Step SP3 of the SyncPlay implementation plan.

What changed

src/Session/SyncPlay/SyncPlayManager.php

  • createGroup() and joinGroup() now accept an optional ?string $connectionId = null 5th parameter and store it in the member record (previously always null).
  • New private connectionToMember: array<string,string> reverse-map maintained on join/leave/create.
  • broadcastToGroup() rewritten to use ConnectionPool::getInstance()->get($connectionId)->send(<flat frame>) delivering flat canonical SyncPlay frames {type, ...data, timestamp} instead of the {type, data, timestamp} wrapper that sendToSession() produced.
  • leaveGroup() cleans up the connectionToMember entry when a member departs.
  • New public onConnectionClose(string $connectionId): void method for WS close handling.

src/Server/WebSocket/WebSocketServer.php

  • onClose() now calls $this->syncPlayManager->onConnectionClose($wsConnection->getId()) before removing the connection from the pool, ensuring disconnects auto-vacate SyncPlay groups and trigger host re-election.

tests/Unit/Session/SyncPlay/SyncPlayManagerTest.php

5 new tests:

  • testCreateGroupStoresConnectionIdOnMemberRecord
  • testJoinGroupStoresConnectionIdOnMemberRecord
  • testOnConnectionCloseRemovesMemberFromGroup
  • testBroadcastToGroupDeliversToAllConnectedMembers (≥2 connections, verifies send() called on each with correct flat frame)
  • testBroadcastToGroupExcludesSpecifiedMemberIds (verifies exclusion logic)

Gate status

Check Result
./vendor/bin/phpstan analyze src/ --level=9 --no-progress ✅ PASS
./vendor/bin/phpcs --standard=PSR12 -n src/ ✅ PASS
./vendor/bin/phpunit tests/Unit/Session/SyncPlay/ ✅ 129/129 PASS
./vendor/bin/phpunit (full suite) ⚠️ Pre-existing failure — ArrTransportInterface not found in WorkermanArrTransportTest.php (unrelated to SP3)

Notes

  • Branched from origin/master (SP1 and SP2 already merged ✅).
  • Broadcast frame format is now flat canonical (matching Connection::sendFlat() and SyncPlay protocol spec).
  • The connectionToMember map enables O(1) member lookup on connection close without scanning all groups.

…up broadcast routing

Implements Step SP3 of the SyncPlay implementation plan.

Changes:
- SyncPlayManager.createGroup() and joinGroup() now accept an optional
  connection_id parameter and store it in the member record (replacing the
  prior hard-coded null).
- A private connectionToMember reverse-map is maintained so that
  ConnectionPool::get($connectionId)->send(<flat frame>) is used for all
  group broadcasts instead of the incorrect sendToSession() wrapper format.
- SyncPlayManager.onConnectionClose($connectionId) is called from
  WebSocketServer::onClose() to automatically vacate the member from their
  group and trigger host re-election if needed.
- WS handlers (handleGroupCreate, handleGroupJoin) now pass
  \$connection->getId() as the connection_id when creating / joining.
- 5 new unit tests covering connection_id binding, onClose leave, and
  broadcast delivery/exclusion with ≥2 connections.

Gate: phpstan level 9 clean, PSR-12 clean, SyncPlay test suite 129/129
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 4 medium · 8 minor

Alerts:
⚠ 12 issues (≤ 0 issues of at least minor severity)

Results:
12 new issues

Category Results
BestPractice 1 medium
Documentation 5 minor
CodeStyle 3 minor
Complexity 3 medium

View in Codacy

🟢 Metrics 24 complexity · 2 duplication

Metric Results
Complexity 24
Duplication 2

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@detain detain merged commit 827c7ac into master Jun 29, 2026
11 of 14 checks passed
@detain detain deleted the fix/server-sp3-syncplay-member-binding branch June 29, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant