# WebSocket Real-time Updates AutoModerate provides real-time updates through WebSocket connections using Socket.IO, allowing you to receive instant notifications about moderation results. ## Connection ### Endpoint ``` ws://localhost:6217/socket.io/ ``` AutoModerate uses Socket.IO for WebSocket communication, which provides automatic fallback options and reconnection. ### JavaScript Client ```html ``` ### Python Client ```python import socketio sio = socketio.Client() @sio.event def connect(): print('Connected to AutoModerate') @sio.event def disconnect(): print('Disconnected from AutoModerate') sio.connect('http://localhost:6217') ``` ## Authentication WebSocket connections require authentication. You must be logged in to the web dashboard to establish a connection. ### Session-based Authentication WebSocket connections require authentication via the web dashboard session. Users must be logged in to establish a connection and can only join project rooms for projects they have access to. ## Project Rooms AutoModerate uses "rooms" to organize real-time updates by project. You must join a project room to receive updates for that specific project. ### Join Project Room ```javascript socket.emit('join_project', { project_id: 'your-project-id' }); // Listen for successful join socket.on('joined_project', function(data) { console.log(`Joined project: ${data.project_id}`); console.log(`Room: ${data.room}`); }); // Handle errors socket.on('error', function(error) { console.error('Error:', error.message); }); ``` ### Leave Project Room ```javascript socket.emit('leave_project', { project_id: 'your-project-id' }); socket.on('left_project', function(data) { console.log(`Left project: ${data.project_id}`); }); ``` ## Event Types ### 1. Connection Events #### Event: `connect` Triggered when the client connects to the server. ```javascript socket.on('connect', function() { console.log('Connected to AutoModerate'); // You can now join project rooms }); ``` #### Event: `connected` Server confirmation of successful connection (only for authenticated users). ```javascript socket.on('connected', function(data) { console.log('Server message:', data.message); }); ``` #### Event: `disconnect` Triggered when the client disconnects from the server. ```javascript socket.on('disconnect', function() { console.log('Disconnected from AutoModerate'); // Handle reconnection logic if needed }); ``` ### 2. Project Room Events #### Event: `joined_project` Confirmation that you've successfully joined a project room. ```javascript socket.on('joined_project', function(data) { console.log('Joined project:', data.project_id); console.log('Room name:', data.room); // You'll now receive updates for this project }); ``` #### Event: `left_project` Confirmation that you've left a project room. ```javascript socket.on('left_project', function(data) { console.log('Left project:', data.project_id); // You'll no longer receive updates for this project }); ``` ### 3. Error Events #### Event: `error` Error messages from the server. ```javascript socket.on('error', function(error) { console.error('WebSocket error:', error.message); // Common error messages: // - "Authentication required" // - "Project ID required" // - "Project not found" // - "Access denied - you are not a member of this project" }); ``` ### 4. Moderation Update Events #### Event: `moderation_update` Receive real-time updates when content is moderated in your project. ```javascript // First, join a project room socket.emit('join_project', {project_id: 'your-project-id'}); // Then listen for moderation updates socket.on('moderation_update', function(data) { console.log('Content moderated:', data); // data contains: content_id, status, decision, processing_time, etc. updateUI(data); }); ``` **Event Data Structure:** ```json { "content_id": "uuid-here", "status": "approved|rejected|flagged", "decision": "approved", "confidence": 0.95, "processing_time": 0.23, "moderator_type": "rule|ai|manual" } ``` ## Complete Implementation Example ### Frontend Dashboard ```html