Skip to content

P2.6: Implement Real-time Scan Updates via WebSockets #32

@Virgo-Alpha

Description

@Virgo-Alpha

To create a more dynamic and engaging user experience, this ticket involves implementing real-time updates using WebSockets. When a user is viewing the results page for a scan that is IN_PROGRESS, they will see live updates pushed from the server without needing to refresh the page.

Implementation Tips

  • Install & Configure Django Channels: Add channels to your INSTALLED_APPS. Set up an ASGI application entrypoint (asgi.py) and configure a channel layer backend. For development, the in-memory backend is fine. For production, you would use a Redis channel layer.
  • Create a Consumer: In your scans app, create a consumers.py file. Define a ScanConsumer that handles WebSocket connections. It should have methods for connect, disconnect, and receiving messages.
  • Channel Groups: When a user connects to the results page for a specific scan (e.g., scans/123/), their WebSocket connection should be added to a unique group for that scan, like scan_123.
  • Send Updates from Celery: The most important part is the integration. Your perform_scan_task Celery task will be long-running. At key points in the task (e.g., "Starting financial analysis...", "Web search complete..."), it will send a message to the channel group for that job.
  • Frontend JavaScript: On your results page, you will need a small amount of JavaScript to open a WebSocket connection to your Django Channels consumer. This script will listen for incoming messages and update the page content dynamically (e.g., by changing a status message or appending to a log).

Acceptance Criteria (Checklist)

  • - Django Channels is installed and configured correctly.
  • - A ScanConsumer is created that can accept WebSocket connections.
  • - The perform_scan_task Celery task sends progress update messages to the correct channel group.
  • - The scan results page uses JavaScript to connect to the WebSocket.
  • - When the Celery task sends an update, it appears on the results page in real-time.

Unit Tests

  • Consumer Tests: Django Channels provides a testing framework (channels.testing.WebsocketCommunicator) to write unit tests for your consumers.
  • Test Connection: Write a test to ensure a user can connect and be added to the correct channel group.
  • Test Message Broadcasting: Write a test for your Celery task. Mock the channel layer's send method and assert that your task calls it with the correct group name and message payload at different stages of its execution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions