Skip to main content

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 token

Socket.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:
The system will assign a unique user ID if none is provided.

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 the CodeType 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:
Common error codes:
  • MESSAGE_CODE_REQUIRED: Missing message code
  • UNSUPPORTED_MESSAGE_TYPE: Unknown message type
  • ROOM_ID_REQUIRED: Room ID not provided
  • FAILED_TO_PROCESS_MESSAGE: General processing error

Connection Lifecycle

The WebSocket connection follows a distinct lifecycle:
  1. Connection: Client establishes connection with optional authentication
  2. Welcome: Server sends a system message confirming connection
  3. Cookie: Server sets a user ID cookie if not present
  4. Interaction: Exchange of messages based on application needs
  5. Disconnection: Triggered by client disconnect or timeout
When a client connects, they’ll receive a welcome message:

Implementation Details

The WebSocket system is built on three key components:
  1. WebSocketManager (utils/websocket_manager.py): Central component for connection management, message routing, and broadcasting
  2. Message Types (models/websocket.py): Pydantic models for typed message validation
  3. Route Handlers (api/routes/websocket.py): FastAPI WebSocket endpoint definitions
The implementation supports both direct point-to-point messages and room-based broadcasting patterns.

Best Practices

When implementing client applications:
  1. Reconnection Logic: Implement automatic reconnection with exponential backoff
  2. Message Validation: Validate outgoing messages against the API schema
  3. Error Handling: Process error responses and retry when appropriate
  4. Event Delegation: Use an event-based architecture to handle different message types
  5. 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