Server Websocket Support - WEKIT-ECS/MIRAGE-XR GitHub Wiki

WebSocket support has been introduced to enable real-time incremental updates in collaborative sessions. Clients can now subscribe to activity-specific WebSocket channels to receive immediate notifications about changes made by others.

Changes

  • 🎯 Dynamic WebSocket Channels

    • Clients connect to ws://127.0.0.1:8000/ws/activities/<activity_id>/ to subscribe to updates for a specific activity.
  • 🛠️ ActivityConsumer WebSocket Consumer

    • Manages WebSocket connections and broadcasts updates to all clients in the activity group.
    • Handles client connect and disconnect events.
  • 🚀 Broadcasting Updates on Activity Modification

    • After a successful activity update, the server broadcasts a message to the relevant WebSocket group.
    • Includes activity_id, action (e.g., "updated"), and the updated data.
  • ⚙️ ASGI and Routing Configuration

    • Configured the ASGI application to support both HTTP and WebSocket protocols.
    • Defined WebSocket URL patterns for activity updates.
  • 🗄️ Channel Layers and Dependencies

    • Configured Redis as the channel layer backend for message passing.
    • Added necessary dependencies: channels, channels-redis, redis, etc.

Related Issue

Closes #6: Retrieve incremental updates for other clients.

Data

Javascript usage example

const socket = new WebSocket(`ws://127.0.0.1:8000/ws/activities/63b76319-68ef-4412-9dd4-bb2a13315ee2/`);

socket.onmessage = function(event) {
    const data = JSON.parse(event.data);
    console.log(`Activity ${data.activity_id} ${data.action}:`, data.data);
};

Reserved event IDs

ID Range Category Description
100-199 Activity-related messages Messages related to activities and operations
200-299 User-related messages Messages related to user actions and events
300-399 Notifications and alerts System-wide notifications and alerts
400-499 File or asset-related messages Messages about file uploads, downloads, or changes
500+ Future expansion Reserved for future events or categories

Specific Events for Each Category

Activity-Related Messages (100-199)

ID Event Name Description
101 activity_created Triggered when a new activity is created.
102 activity_updated Triggered when an activity is updated.
103 activity_deleted Triggered when an activity is deleted.
104 activity_retrieved Triggered when an activity is retrieved by ID/name.
105 all_activities_retrieved Triggered when all activities are retrieved.
111 activity_config_updated Triggered when an activity configuration is updated.
112 activity_config_retrieved Triggered when an activity configuration is retrieved.
113 activity_hash_retrieved Triggered when an activity hash is retrieved.
114 all_activity_hashes_retrieved Triggered when all activity hashes are retrieved.

User-Related Messages (200-299)

ID Event Name Description
201 user_joined Triggered when a user joins an activity or session.
202 user_left Triggered when a user leaves an activity or session.
203 user_status_updated Triggered when a user's status is updated.

Notifications and Alerts (300-399)

ID Event Name Description
301 system_alert Triggered when a system-wide alert is sent.
302 notification_sent Triggered when a notification is sent to a user.

File or Asset-Related Messages (400-499)

ID Event Name Description
401 file_uploaded Triggered when a file is uploaded to the server.
402 file_deleted Triggered when a file is deleted.
403 file_downloaded Triggered when a file is downloaded by a user.

Example

{
    "id": 102,
    "activity_id": "63b76319-68ef-4412-9dd4-bb2a13315ee2",
    "action": "updated",
    "updated_fields": {
        "name": "Updated Activity Name",
        "description": "Updated description of the activity",
        "steps": [
            {
                "id": "d00f6246-08b5-4cc2-8db0-0c2344589691",
                "description": "Updated step description"
            }
        ]
    }
}