Skip to content

Commit 274326f

Browse files
committed
Pass user_id via query param in WebSocket connection
Refactored backend WebSocket endpoint to accept user_id as a query parameter instead of extracting it from headers. Updated frontend WebSocketService to append user_id to the connection URL, ensuring consistent user identification for real-time updates.
1 parent c702314 commit 274326f

File tree

2 files changed

+5
-14
lines changed

2 files changed

+5
-14
lines changed

src/backend/v3/api/router.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,13 @@
5151

5252

5353
@app_v3.websocket("/socket/{process_id}")
54-
async def start_comms(websocket: WebSocket, process_id: str):
54+
async def start_comms(websocket: WebSocket, process_id: str, user_id: str = Query(None)):
5555
"""Web-Socket endpoint for real-time process status updates."""
5656

5757
# Always accept the WebSocket connection first
5858
await websocket.accept()
5959

60-
user_id = None
61-
try:
62-
# WebSocket headers are different, try to get user info
63-
headers = dict(websocket.headers)
64-
authenticated_user = get_authenticated_user_details(request_headers=headers)
65-
user_id = authenticated_user.get("user_principal_id")
66-
if not user_id:
67-
user_id = "00000000-0000-0000-0000-000000000000"
68-
except Exception as e:
69-
logging.warning(f"Could not extract user from WebSocket headers: {e}")
70-
user_id = "00000000-0000-0000-0000-000000000000"
60+
user_id = user_id or "00000000-0000-0000-0000-000000000000"
7161

7262
current_user_id.set(user_id)
7363

src/frontend/src/services/WebSocketService.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getApiUrl, headerBuilder } from '../api/config';
1+
import { getApiUrl, getUserId, headerBuilder } from '../api/config';
22
import { PlanDataService } from './PlanDataService';
33
import { MPlanData, ParsedPlanApprovalRequest, StreamingPlanUpdate, StreamMessage, WebsocketMessageType } from '../models';
44

@@ -25,9 +25,10 @@ class WebSocketService {
2525
// Leave ws/wss as-is; anything else is assumed already correct
2626

2727
// Decide path addition
28+
let userId = getUserId();
2829
const hasApiSegment = /\/api(\/|$)/i.test(base);
2930
const socketPath = hasApiSegment ? '/v3/socket' : '/api/v3/socket';
30-
const url = `${base}${socketPath}${processId ? `/${processId}` : `/${planId}`}`;
31+
const url = `${base}${socketPath}${processId ? `/${processId}` : `/${planId}`}&user_id=${userId || ''}`;
3132
console.log("Constructed WebSocket URL:", url);
3233
return url;
3334
}

0 commit comments

Comments
 (0)