Skip to content

Conversation

@calebbourg
Copy link
Collaborator

@calebbourg calebbourg commented Nov 15, 2025

Description

This PR implements the foundational infrastructure for Server-Sent Events (SSE) to enable real-time, one-way communication from
the backend to authenticated users. This establishes the core architecture needed for future real-time features like action
notifications, coaching session updates, and system announcements.

GitHub Issue: [Closes|Fixes|Resolves] #your GitHub issue number here

Changes

  • New sse crate: Standalone crate with generic types (String IDs, JSON payloads) to avoid circular dependencies
    • ConnectionRegistry: Dual-index architecture using DashMap for O(1) concurrent lookups by connection_id and user_id
    • Manager: High-level API for connection lifecycle and message routing
    • Message types: Event enum with action, agreement, goal, and system event variants
    • MessageScope: User-targeted and broadcast message delivery
  • SSE HTTP endpoint (web/src/sse/handler.rs): GET /sse endpoint for establishing long-lived SSE connections
    • Async streaming using async_stream::stream! and Tokio channels
    • Automatic connection cleanup on disconnect
    • One connection per authenticated user, persisting across page navigation
  • AppState integration: Added sse_manager: Arc<sse::Manager> to service layer's AppState
    • Enables controllers to send SSE events via app_state.sse_manager.send_message()
    • Manager initialized at application startup in main.rs and seed_db.rs
  • Infrastructure configuration:
    • Nginx configuration for /api/sse endpoint (24h timeout, no buffering, chunked encoding)
    • Docker Compose warning about single-instance limitation (in-memory connection tracking)
  • Testing tool (sse-test-client): CLI tool for integration testing SSE functionality
    • Connection stability testing
    • Force logout scenario
    • Multiple test scenarios with varying permission requirements
  • Architecture documentation updates: Updated crate dependency graph, system architecture diagram, and network flow diagram
    to reflect SSE components

Testing Strategy

  1. Manual testing with sse-test-client:
    cd sse-test-client
    cargo run
    # Select "Connection Test" to verify basic SSE connectivity
  2. Unit tests: Updated auth middleware tests to include SSE manager initialization
  3. Integration testing: Use sse-test-client scenarios to verify:
    - Connection establishment and stability
    - Multiple concurrent user connections
    - Graceful connection cleanup on disconnect

Concerns

  • Single-instance limitation: SSE connections are tracked in-memory using DashMap, which means the application cannot scale
    horizontally without implementing Redis Pub/Sub for cross-instance event distribution
    • Warning added to docker-compose.yaml
    • Symptom if violated: events randomly fail to deliver with multiple replicas
  • No event persistence: All SSE events are ephemeral - offline users miss events and see fresh data on next page load
  • No message delivery guarantees: If channel send fails, events are dropped (logged but not retried)
  • Generic types in SSE crate: Using String for IDs and serde_json::Value for payloads to avoid circular dependencies - type
    safety enforced at web layer boundaries

calebbourg and others added 15 commits November 15, 2025 11:20
better connection lookup and remove the need for unwrap
- Add critical warning to docker-compose.yaml about SSE single-instance limitation
  * SSE connections tracked in-memory with DashMap
  * Must not scale horizontally without Redis Pub/Sub
  * Warns about symptom: events randomly fail with multiple replicas

- Add nginx configuration for /api/sse endpoint
  * Disable proxy buffering for immediate event streaming
  * Set 24h read timeout for long-lived SSE connections
  * Enable chunked transfer encoding
  * Clear connection header for proper streaming
  * Add CORS headers for credential support

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

Co-Authored-By: Claude <noreply@anthropic.com>
Create standalone SSE crate to avoid circular dependencies between
service and web layers. Uses generic types (String for IDs,
serde_json::Value for payloads) to remain independent of domain models.

Key components:
- ConnectionRegistry: Dual-index (connection_id, user_id) architecture
  using DashMap for O(1) concurrent lookups
- Manager: High-level API for connection lifecycle and message routing
- Message types: Event enum with action, agreement, goal, and system events
- MessageScope: User-targeted and broadcast message delivery

Architecture decisions:
- In-memory connection tracking (single-instance only)
- Generic types to avoid domain dependency
- Thread-safe using DashMap and Arc
- Tokio channels for event distribution

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

Co-Authored-By: Claude <noreply@anthropic.com>
Add SSE manager to the service layer's AppState to enable real-time
event distribution throughout the application. The manager is wrapped
in Arc for thread-safe sharing across request handlers.

Changes:
- Add sse dependency to service crate
- Add sse_manager: Arc<sse::Manager> field to AppState
- Update AppState::new() to accept sse_manager parameter
- Make sse_manager publicly accessible via getter

This allows controllers to send SSE events by calling
app_state.sse_manager.send_message() with appropriate message scope.

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

Co-Authored-By: Claude <noreply@anthropic.com>
Implement Axum SSE handler that establishes long-lived HTTP connections
for server-sent events. One connection per authenticated user, persisting
across page navigation.

Implementation:
- Handler at GET /sse (behind authentication middleware)
- Uses async_stream::stream! for event streaming
- Registers user connection with SSE manager
- Automatic cleanup when connection closes
- Returns Sse<impl Stream<Item = Result<Event, Infallible>>>
- Keep-alive enabled with default settings

Technical details:
- Tokio unbounded channel receives events from manager
- Stream yields events as they arrive from channel
- Connection ID generated server-side for lifecycle tracking
- Converts domain::Id to String for SSE layer compatibility

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

Co-Authored-By: Claude <noreply@anthropic.com>
Create and pass SSE manager instance to AppState in both the main
application and database seeding utility.

Changes:
- main.rs: Initialize Arc<sse::Manager::new()> and pass to AppState
- seed_db.rs: Initialize SSE manager for test data seeding context

The manager is created once at startup and shared across all request
handlers via Arc for thread-safe access.

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

Co-Authored-By: Claude <noreply@anthropic.com>
Update three authentication middleware tests to initialize SSE manager
when creating AppState, matching the new 3-parameter constructor signature.

Fixed tests:
- test_require_auth_returns_401_with_no_session
- test_require_auth_returns_401_with_invalid_session_cookie
- test_require_auth_allows_authenticated_request_to_proceed

Each test now creates Arc<sse::Manager::new()> before constructing
AppState to maintain test isolation while matching production code.

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

Co-Authored-By: Claude <noreply@anthropic.com>
Update project documentation to reflect the new SSE (Server-Sent Events)
real-time communication infrastructure.

Changes:
- README.md: Add sse crate to project directory structure
- crate_dependency_graph.md: Add sse crate dependencies (web→sse, service→sse)
- system_architecture_diagram.md: Add SSE Handler and SSE Manager components
  with event flow from domain layer
- network_flow_diagram.md: Document SSE endpoint configuration and
  single-instance scaling limitation

Key documentation notes:
- SSE uses in-memory connection tracking (single-instance only)
- Nginx configured for long-lived connections (24h timeout, no buffering)
- Generic types used in sse crate to avoid circular dependencies

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

Co-Authored-By: Claude <noreply@anthropic.com>
…ling

- Change login endpoint from /user_sessions to /login
- Use form-encoded data instead of JSON for login requests
- Update cookie name from session_id to id throughout codebase
- Parse ApiResponse wrapper structure for user data
- Improve error messages with response body details

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

Co-Authored-By: Claude <noreply@anthropic.com>
- Add x-version: 1.0.0-beta1 header to all API requests
- Fix coaching relationships endpoint to include organization_id
- Add get_user_organizations method to fetch user's orgs
- Parse ApiResponse wrapper for all endpoint responses
- Update all cookie headers to use 'id' instead of 'session_id'

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

Co-Authored-By: Claude <noreply@anthropic.com>
Add test_connection function that verifies basic SSE connectivity
without requiring coaching data. This scenario:
- Establishes SSE connections for both users
- Waits 2 seconds to verify connections stay alive
- Reports success if connections remain stable

This allows testing SSE infrastructure without admin permissions.

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

Co-Authored-By: Claude <noreply@anthropic.com>
- Add ConnectionTest scenario choice to CLI
- Add ForceLogoutTest scenario choice to CLI
- Make test environment setup conditional (skip for ConnectionTest)
- Update All scenario to include ConnectionTest
- Improve scenario descriptions with requirements

ConnectionTest can run without admin permissions since it doesn't
require creating coaching relationships or sessions.

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

Co-Authored-By: Claude <noreply@anthropic.com>
…rceLogoutTest

- Remove unused test_env parameter from test_force_logout function
- Skip test environment setup for ForceLogoutTest scenario
- Fix force_logout cookie header to use 'id' instead of 'session_id'
- Update README with new connection-test scenario documentation
- Clarify permission requirements for each test scenario
- Add example output for connection test

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

Co-Authored-By: Claude <noreply@anthropic.com>
@calebbourg calebbourg marked this pull request as ready for review December 1, 2025 14:24
@calebbourg calebbourg self-assigned this Dec 1, 2025
@calebbourg calebbourg requested a review from jhodapp December 1, 2025 14:25
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