Skip to content

feat(panels): add WebSocket Panel for real-time connection tracking#19

Merged
JacobCoffee merged 5 commits intomainfrom
websocket-panel
Nov 30, 2025
Merged

feat(panels): add WebSocket Panel for real-time connection tracking#19
JacobCoffee merged 5 commits intomainfrom
websocket-panel

Conversation

@JacobCoffee
Copy link
Owner

Summary

Implements Phase 10.3 from PLAN.md - WebSocket Panel with full connection and message tracking capabilities.

Features

  • 🔌 Connection Tracking: Real-time WebSocket connection lifecycle (connect → accept → messages → close)
  • 📨 Message Logging: Bidirectional message capture with timestamps, sizes, and content preview
  • 📊 Statistics: Total connections, messages sent/received, bytes transferred
  • 🔄 Auto-Detection: Automatically adds WebSocketPanel when @websocket handlers or WebsocketListener classes detected
  • ⚙️ Configurable: Message buffer size, content truncation, TTL, binary preview settings

Configuration Options

websocket_tracking_enabled: bool = True
websocket_max_connections: int = 50
websocket_max_messages_per_connection: int = 100
websocket_max_message_size: int = 10240  # 10KB
websocket_binary_preview_size: int = 256  # bytes
websocket_connection_ttl: int = 3600  # 1 hour

Files Changed

  • src/debug_toolbar/core/panels/websocket.py - New panel with dataclasses
  • src/debug_toolbar/core/config.py - WebSocket configuration options
  • src/debug_toolbar/litestar/middleware.py - WebSocket scope handling
  • src/debug_toolbar/litestar/plugin.py - Auto-detection logic
  • src/debug_toolbar/templates/panels/websocket.html - Panel UI
  • examples/websocket_panel_example.py - Demo with Echo & Chat

Test plan

  • 30 unit tests for data models and panel (100% coverage)
  • 15 integration tests for middleware WebSocket handling
  • All existing tests still pass (569 passed)
  • make lint passes
  • make type-check passes (no new errors)
  • Manual testing with example app

🤖 Generated with Claude Code

Implements Phase 10.3 from PLAN.md - WebSocket Panel with:

## Core Features
- WebSocketMessage/WebSocketConnection dataclasses for tracking
- WebSocketPanel with class-level storage for connection state
- Message logging (sent/received) with timestamps and sizes
- Connection lifecycle tracking (connecting → connected → closed)
- Circular buffer with configurable max messages per connection
- Content truncation for large messages (configurable size limits)

## Middleware Integration
- WebSocket scope detection in Litestar middleware
- Send/receive wrappers for message interception
- Close code and reason capture
- Graceful error handling (tracking never breaks WebSocket)

## Auto-Detection
- Plugin auto-detects WebSocket handlers (@websocket, WebsocketListener)
- Automatically adds WebSocketPanel when WebSocket usage detected
- Works with both decorator and class-based handlers

## Configuration Options
- websocket_tracking_enabled (default: True)
- websocket_max_connections (default: 50)
- websocket_max_messages_per_connection (default: 100)
- websocket_max_message_size (default: 10KB)
- websocket_binary_preview_size (default: 256 bytes)
- websocket_connection_ttl (default: 1 hour)

## Testing
- 30 unit tests for data models and panel
- 15 integration tests for middleware
- 100% coverage on websocket.py module

Includes example app with Echo and Chat WebSocket handlers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings November 30, 2025 21:21
@github-actions
Copy link
Contributor

github-actions bot commented Nov 30, 2025

PR Preview Action v1.6.3

🚀 View preview at
https://JacobCoffee.github.io/debug-toolbar/pr-preview/pr-19/

Built to branch gh-pages at 2025-11-30 23:13 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

- Change Makefile example targets to use `uv run python` instead of
  `uv run` to use project venv instead of inline script dependencies
- Add example-websocket target
- Fix websocket example to not use reload mode (avoids module path issues)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements Phase 10.3 from PLAN.md by adding comprehensive WebSocket connection tracking to the debug toolbar. The implementation includes a new WebSocketPanel that monitors WebSocket lifecycle events (connect, message exchange, disconnect), tracks bidirectional message flow with content previews, and provides real-time statistics. The panel is automatically added when WebSocket handlers are detected in the application.

Key Changes

  • WebSocket Panel Implementation: New panel with connection tracking, message logging, and statistics (connections, messages, bytes transferred)
  • Middleware Enhancement: Extended DebugToolbarMiddleware to handle WebSocket scope with message interception wrappers
  • Auto-Detection: Plugin automatically detects WebSocket handlers (@websocket decorator, WebsocketListener classes) and adds the panel

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 21 comments.

Show a summary per file
File Description
src/debug_toolbar/core/panels/websocket.py Core panel implementation with WebSocketMessage, WebSocketConnection dataclasses and thread-safe connection tracking
src/debug_toolbar/core/panels/__init__.py Exports new WebSocketPanel
src/debug_toolbar/core/config.py Adds WebSocket configuration options (tracking, limits, TTL, preview settings)
src/debug_toolbar/litestar/middleware.py Extends middleware to handle WebSocket scope with send/receive wrappers for message tracking
src/debug_toolbar/litestar/plugin.py Implements auto-detection logic for WebSocket handlers
src/debug_toolbar/templates/panels/websocket.html Responsive UI template showing connection tables, message timeline, and statistics
examples/websocket_panel_example.py Demo application with echo and chat WebSocket handlers
tests/core/panels/test_websocket.py Unit tests for panel, dataclasses, and methods (30 tests)
tests/integration/test_websocket_integration.py Integration tests for middleware WebSocket tracking (15 tests)
tests/core/panels/__init__.py Test package initialization
tests/core/__init__.py Test package initialization
Comments suppressed due to low confidence (1)

examples/websocket_panel_example.py:35

  • Import of 'asyncio' is not used.
import asyncio

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

JacobCoffee and others added 2 commits November 30, 2025 16:46
- Remove duplicate Any import in middleware.py
- Implement max_connections enforcement to prevent unbounded memory growth
- Remove unused config options (websocket_binary_preview_size, websocket_show_binary_preview)
- Fix template key mismatches (type/size/preview)
- Add debug logging to except clauses in example
- Simplify redundant handler.__wrapped__ check in plugin.py
- Use cast() instead of type: ignore for message_type
- Extract duplicate code between send/receive wrappers into helper method
- Fix docstring - remove binary WebSocket reference
- Add WebSocket panel screenshot to README and docs
- Update PLAN.md to reflect completion of Phase 10.3

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Update CI workflow to use ruff 0.14.0 instead of 0.9.0
- Add per-file ignore for TC001/TC002 in handlers.py (runtime import)
- Fix import order in handlers.py

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings November 30, 2025 22:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 18 out of 19 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Move logging import to module level in handlers.py
- Use dynamic ws/wss protocol for HTTPS support
- Fix threading lock with asyncio queue - copy subscribers before iterating

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@JacobCoffee JacobCoffee enabled auto-merge (squash) November 30, 2025 23:20
@JacobCoffee JacobCoffee merged commit ac1b97f into main Nov 30, 2025
18 checks passed
@JacobCoffee JacobCoffee deleted the websocket-panel branch November 30, 2025 23:20
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.

2 participants