[BACK-WS-01] WebSocket Notification Service and Media Processing Queue Integration
Backend: Architecture
NoteTask Brief
- Repository / Component:
food_tracker_backend(Go). - Stack: Go (Gorilla WebSocket / Centrifugo), Apache Kafka, PostgreSQL.
- Task Type: Backend (Architecture / Asynchronous Pipeline).
- Status: Under Analysis
Task Description
Once a receipt or audio file is uploaded, the entire pipeline (AI -> Validation -> Kafka Topic -> Postgres Write by Worker) executes asynchronously. It is necessary to implement a WebSocket server to deliver real-time transaction completion events directly to the client.
Step-by-Step Instructions (Go)
- WS Endpoint Implementation: Create a handler at
/api/v1/wsthat performs an HTTP connection upgrade to WebSocket. Ensure validation of the user’s JWT token during the initial connection handshake. - Session Manager (Hub): Implement an active connection management structure (
Hub / Pool) that maps aUserIDto its corresponding active network connection state (conn). - Reading from the Kafka Topic (Worker): The worker responsible for consuming the final processing results topic (
receipts_processed) must emit an internal Event Bus message immediately after executing a successfulINSERTstatement into the PostgreSQL products table. - Streaming the Event: Upon receiving a notification from the internal channel:
- Verify if a WebSocket session is currently active for the given
UserID. - If YES: Push a JSON payload to the active socket:
{"event": "PRODUCTS_PROCESSED", "transaction_id": "BILL-XXX", "count": 5}. - If NO (e.g., the user closed the application): Generate a background task routed to the Push Notification Service (FCM) to trigger a system push notification instead.
- Verify if a WebSocket session is currently active for the given