Skip to content

feat: implement WebSocket handler and event system#118

Open
2witstudios wants to merge 3 commits intomainfrom
ppg/issue-73-ws-handler
Open

feat: implement WebSocket handler and event system#118
2witstudios wants to merge 3 commits intomainfrom
ppg/issue-73-ws-handler

Conversation

@2witstudios
Copy link
Owner

Summary

  • Adds WebSocket endpoint (ws://<host>:<port>/ws?token=<token>) with token-based auth via query parameter
  • Typed discriminated union event system for server→client events (pong, manifest:updated, agent:status, terminal:output, error) and client→server commands (ping, terminal:subscribe, terminal:unsubscribe, terminal:input)
  • Client set tracking with per-client terminal subscription state, broadcast() and sendEvent() helpers, and proper cleanup on disconnect

Files

  • src/server/ws/events.ts — Command/event type definitions, parser, serializer
  • src/server/ws/handler.ts — WebSocket handler with auth, client tracking, command dispatch
  • src/server/ws/handler.test.ts — 26 tests covering auth, commands, broadcast, cleanup

Test plan

  • npm run typecheck passes (only pre-existing spawn.test.ts error)
  • npm test — all 244 tests pass (22 files)
  • Connection accepted with valid token
  • Connection rejected with invalid/missing token or wrong path
  • Async token validation supported
  • Ping/pong round-trip works
  • Invalid commands return typed error events
  • Terminal subscribe/unsubscribe modifies per-client state
  • Terminal input dispatches to onTerminalInput callback
  • Broadcast reaches all connected clients
  • sendEvent targets specific client only
  • Client removal on disconnect
  • close() terminates all clients and cleans up

Closes #73

Add WebSocket endpoint with token auth, client tracking, command dispatch,
and typed discriminated union events for real-time server-client communication.

- ws://<host>:<port>/ws?token=<token> endpoint via HTTP upgrade
- Token validation from query parameter (sync and async)
- Client set with per-client terminal subscription state
- Command dispatch: ping, terminal:subscribe/unsubscribe, terminal:input
- Server events: pong, manifest:updated, agent:status, terminal:output, error
- broadcast() and sendEvent() helpers
- Proper cleanup on disconnect and server shutdown
- 26 tests covering auth, commands, broadcast, and cleanup

Closes #73
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai
Copy link

coderabbitai bot commented Feb 27, 2026

Warning

Rate limit exceeded

@2witstudios has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 12 minutes and 57 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 34deb69 and 8e4159b.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (5)
  • package.json
  • src/commands/spawn.test.ts
  • src/server/ws/events.ts
  • src/server/ws/handler.test.ts
  • src/server/ws/handler.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ppg/issue-73-ws-handler

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

P1: Remove upgrade listener from server on close() to prevent listener
leak if handler is recreated on the same server.

P2: Pre-serialize event once in broadcast() instead of per-client.
P2: Add maxPayload (64KB) to WebSocketServer to prevent OOM.
P2: Replace timer-based test synchronization (setTimeout 50ms) with
deterministic ping/pong round-trip barriers.

P3: Remove unused beforeEach import.
P3: Strengthen sendEvent specificity test to verify ws2 does not receive.
P3: Add sendEvent skip test for closed socket readyState guard.
P3: Add tests for onTerminalInput not provided (no-op path).
P3: Add tests for sync and async onTerminalInput throw/reject paths.
P3: Add test verifying close() removes upgrade listener.
P3: Fix close() ordering: close clients → close WSS → clear set.
P3: Wrap synchronous onTerminalInput throws in try/catch.
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.

Implement WebSocket handler and event system

1 participant