Skip to content

Fix WebSocket real-time updates - Connection refused errors #87

Description

@frankbria

Problem

E2E test for WebSocket real-time updates is failing with ERR_CONNECTION_REFUSED, indicating the WebSocket connection is not being established properly.

Current Behavior

From test output:

[error] Failed to load resource: net::ERR_CONNECTION_REFUSED
[log] WebSocket connected

The frontend WebSocket client attempts to connect, shows "connected" in logs, but the connection is actually failing with ERR_CONNECTION_REFUSED.

Failed Test

File: tests/e2e/test_dashboard.spec.ts
Test: should receive real-time updates via WebSocket (line 221)

Expected: Dashboard receives real-time agent/task updates via WebSocket
Actual: Connection refused, no updates received

Root Cause Investigation

1. Verify WebSocket endpoint exists in backend

Check if backend has WebSocket route configured:

# Search for WebSocket route in backend
grep -r "websocket" codeframe/ui/server.py
grep -r "WebSocket" codeframe/ui/server.py

2. Check WebSocket connection URL

Frontend WebSocket client configuration:

// web-ui/src/lib/websocket.ts
const wsUrl = `ws://localhost:${port}/ws`;

Verify:

  • Port matches backend server port (8080)
  • Path matches backend WebSocket route (/ws or similar)
  • Protocol is ws:// (not wss:// for local testing)

3. Test WebSocket manually

# Start backend
cd /home/frankbria/projects/codeframe
uv run uvicorn codeframe.ui.server:app --port 8080

# In another terminal, test WebSocket
wscat -c ws://localhost:8080/ws

Expected: Connection established
Actual: ?

4. Check backend WebSocket implementation

Expected FastAPI WebSocket route:

from fastapi import WebSocket

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    # ... send updates

Likely Causes

  1. WebSocket route missing - Backend doesn't have /ws endpoint
  2. Wrong port - Frontend connecting to wrong port
  3. Wrong path - Frontend using /websocket but backend expects /ws
  4. CORS issue - WebSocket blocked by CORS policy
  5. Backend not running - WebSocket server not started (unlikely, HTTP works)

Expected Behavior

  1. Frontend opens WebSocket connection to ws://localhost:8080/ws
  2. Backend accepts connection
  3. Backend sends real-time events:
    • agent_created
    • agent_status_changed
    • task_assigned
    • task_status_changed
    • blocker_created
    • blocker_resolved
    • etc.
  4. Frontend AgentStateProvider receives events and updates state
  5. Dashboard UI updates in real-time

Debugging Steps

  1. Check if WebSocket endpoint exists:

    grep -A 10 "websocket" codeframe/ui/server.py
  2. Check frontend WebSocket URL:

    grep -r "getWebSocketClient\|new WebSocket" web-ui/src/
  3. Monitor network traffic during test:

    • Open Chrome DevTools → Network tab
    • Filter by "WS" (WebSocket)
    • Run E2E test
    • Check if WebSocket connection attempted and what error occurs
  4. Add debug logging:

    // web-ui/src/lib/websocket.ts
    console.log('WebSocket connecting to:', wsUrl);
    ws.onopen = () => console.log('WebSocket OPEN');
    ws.onerror = (e) => console.error('WebSocket ERROR:', e);
    ws.onclose = (e) => console.log('WebSocket CLOSE:', e.code, e.reason);

Impact

Blocks 1/12 failed E2E tests - lowest priority but important for real-time UX.

Acceptance Criteria

  • WebSocket endpoint exists at /ws in backend
  • Frontend connects successfully (no ERR_CONNECTION_REFUSED)
  • Real-time events sent from backend to frontend
  • Dashboard updates in real-time when events received
  • E2E test "should receive real-time updates via WebSocket" passes

Related Files

  • web-ui/src/lib/websocket.ts - WebSocket client
  • web-ui/src/components/AgentStateProvider.tsx - WebSocket event handler
  • codeframe/ui/server.py - Backend WebSocket endpoint (if exists)
  • tests/e2e/test_dashboard.spec.ts:221 - Failing test

References

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions