WebSocket API
ProductFlo implements a robust WebSocket system that supports both native WebSocket and Socket.IO protocols for real-time bidirectional communication. This unified interface powers various interactive features including chat, AI interactions, collaborative editing, and system notifications.Connection Endpoints
Main WebSocket
/ws - Primary WebSocket endpoint (no authentication required)Secure WebSocket
/ws/secure - Authenticated WebSocket endpoint requiring tokenSocket.IO
/ws - Socket.IO compatible endpoint (conditionally mounted at runtime)Message Structure
All WebSocket messages follow a consistent structure:Connection Examples
Standard WebSocket (Non-Authenticated)
Secure WebSocket (Authenticated)
Socket.IO Client
Authentication
WebSocket connections support multiple authentication methods:Secure Endpoint
For authenticated connections, use the/ws/secure endpoint which requires a token:
Non-Authenticated Connection
For connections that don’t require authentication, use the standard/ws endpoint:
Socket.IO Authentication
When using Socket.IO, provide authentication information in headers or cookies:Message Types (CodeType)
The WebSocket system uses a set of predefined message types through theCodeType enum:
Sending Types (Client to Server)
string
Regular chat messages
string
AI assistant chat interaction
string
Product idea generation
string
Document generation request
string
Engineering-specific requests
Receiving Types (Server to Client)
string
Response to chat messages
string
Product idea generation response
string
Document generation response
string
Engineering-specific responses
string
System notifications and status updates
string
User notifications
string
Debug and process logs
Status Types
All responses include a status field:string
Operation completed successfully
string
Error occurred during operation
string
Operation is still in progress
Basic Usage Examples
Chat Message
AI Assistant Interaction
Document Generation Request
Event Handling Example
Error Handling
Error responses follow a standardized format:MESSAGE_CODE_REQUIRED: Missing message codeUNSUPPORTED_MESSAGE_TYPE: Unknown message typeROOM_ID_REQUIRED: Room ID not providedFAILED_TO_PROCESS_MESSAGE: General processing error
Connection Lifecycle
The WebSocket connection follows a distinct lifecycle:- Connection: Client establishes connection with optional authentication
- Welcome: Server sends a system message confirming connection
- Cookie: Server sets a user ID cookie if not present
- Interaction: Exchange of messages based on application needs
- Disconnection: Triggered by client disconnect or timeout
Implementation Details
The WebSocket system is built on three key components:-
WebSocketManager (
utils/websocket_manager.py): Central component for connection management, message routing, and broadcasting -
Message Types (
models/websocket.py): Pydantic models for typed message validation -
Route Handlers (
api/routes/websocket.py): FastAPI WebSocket endpoint definitions
Best Practices
When implementing client applications:- Reconnection Logic: Implement automatic reconnection with exponential backoff
- Message Validation: Validate outgoing messages against the API schema
- Error Handling: Process error responses and retry when appropriate
- Event Delegation: Use an event-based architecture to handle different message types
- Typing Indicators: Send typing indicators for improved user experience
Next Steps
Message Types
View detailed message type documentation
Chat
Learn about chat-specific features
AI Communication
Explore AI integration capabilities