WebSocket Events
Receive real-time events for discovery jobs, business service health, new P1 findings, your in-app notifications and your reports over a persistent WebSocket connection.
Connecting to the WebSocket
AsPeekIt exposes a WebSocket endpoint for real-time event delivery. Connect to the notifications endpoint using a standard WebSocket client. Authentication is required: pass a valid JWT access token in the token query parameter (ws[s]://host/ws/notifications/?token=<access token>). A browser session cookie also works. Without a valid user the socket is accepted and immediately closed with code 4401, so clients can tell an authentication failure from a network error. Because the token is only checked during the handshake, clients should obtain a fresh access token (via /api/v1/auth/token/refresh/) before reconnecting. The connection URL uses the ws:// scheme for unencrypted connections or wss:// for TLS-secured connections. Upon a successful connection, the client is automatically added to the "notifications" group (platform-wide events) and to its personal "user_<id>" group (events of that user only).
// Connect to the WebSocket notifications endpoint
const ws = new WebSocket("wss://idp.example.com/ws/notifications/?token=" + encodeURIComponent(accessToken));
ws.onopen = () => {
console.log("Connected to AsPeekIt notifications");
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Event received:", data.type, data);
};
ws.onclose = (event) => {
console.log("Disconnected:", event.code, event.reason);
};Transport & Infrastructure
The WebSocket layer is built on Django Channels with a Redis backend for cross-process message routing. Django Channels manages the WebSocket lifecycle (connect, receive, disconnect) while Redis serves as the channel layer, enabling messages to be broadcast from any worker process to all connected clients. This architecture ensures that events triggered by background Celery workers (such as discovery job completions) are delivered to WebSocket clients in real time.
- Framework: Django Channels (ASGI)
- Channel layer: Redis-backed for horizontal scaling
- Protocol: standard WebSocket (RFC 6455)
- Message format: JSON-encoded text frames
Notification Group
Every authenticated client is automatically joined to the "notifications" group, which receives the platform-wide events: discovery job lifecycle, business service health changes and new P1 findings. No subscription is required.
Personal Group
Every authenticated client is also joined to the personal group of its user, "user_<id>" (every open tab of the same user is in it). Personal events are sent only to this group, never to the "notifications" group: another user never receives them.
- notification_created: a new in-app notification (/docs/notifications/in-app) of the user, with the notification and the new unread count
- notifications_read: the user marked notifications as read (POST /api/v1/notifications/mark-read/), with the new unread count; other tabs refresh their bell
- report_ready / report_failed: the outcome of a report requested by the user. report_type is Executive, Operational or Technical for the infrastructure reports and Compliance for the compliance report (/docs/security/compliance-report), sent by email as compliance-report-YYYYmmdd-HHMM.pdf or .html; a report task queued by an older version (without the requesting user) sends no message
// notification_created
{
"type": "notification_created",
"notification": {
"id": 42,
"event_type": "finding.created",
"title": "CVE-2026-1234 · Critical",
"body": "3 hosts affected · Heap overflow in the parser",
"link": "/dashboard/security?cve=CVE-2026-1234&status=open",
"read_at": null,
"created_at": "2026-11-28T10:00:00Z"
},
"unread_count": 5
}
// notifications_read
{
"type": "notifications_read",
"unread_count": 0
}
// report_ready
{
"type": "report_ready",
"task_id": "3f1c…",
"report_type": "Executive",
"filename": "aspeekit-report-executive-20261128-1000.pdf",
"total_assets": 120,
"email_sent": true
}
// report_failed
{
"type": "report_failed",
"task_id": "3f1c…",
"error": "No active assets found."
}
// report_ready of a compliance report
{
"type": "report_ready",
"task_id": "9a2e…",
"report_type": "Compliance",
"filename": "compliance-report-20261129-1030.pdf",
"total_assets": 48,
"email_sent": true
}Discovery Events
Sent to the notifications group when a discovery job starts, completes or fails, so clients can refresh job lists without polling. environment is the name of the discovery zone, job_id the job id as a string.
// discovery_started
{ "type": "discovery_started", "environment": "Production Kubernetes", "job_id": "257" }
// discovery_completed
{ "type": "discovery_completed", "environment": "Production Kubernetes", "job_id": "257", "assets_discovered": 48 }
// discovery_failed (error cut at 200 characters)
{ "type": "discovery_failed", "environment": "Staging GCP", "job_id": "258", "error": "Authentication failed: invalid service account credentials" }Health Events
Two platform events keep the Cruscotto (/docs/discovery/cruscotto) up to date without waiting for its 30-second polling. Both go to the shared notifications group (never to the asset groups), carry ids and statuses only (no names, causes or other sensitive data) and list at most 50 entries, while count is always the total. Nothing is sent when nothing changed. The web client refreshes the affected queries and shows no toast (the in-app notifications have their own).
- service_health_changed: one message per health computation of the business services (about every 5 minutes) when at least one service changed status or was deactivated (to_status empty); the client refreshes the Cruscotto, the health index, the services and their map, the service health and the topology health overlay
- finding_p1_created: one message per committed chunk of the priority recomputation (up to 2000 findings per chunk, so a large recomputation can send several), listing the findings of that chunk that became P1 (new findings or findings promoted from another band); a finding already P1 is not listed again; the client refreshes the Cruscotto, the health index, the security findings and dashboard, the topology health overlay and the service map
Note: After every (re)connection the client also refreshes the Cruscotto, the health index, the services and their map, the service health and the topology health overlay, to catch the service_health_changed events missed while the socket was down.
// service_health_changed
{
"type": "service_health_changed",
"count": 2,
"changes": [
{"service_id": 3, "from_status": "healthy", "to_status": "critical"},
{"service_id": 8, "from_status": "degraded", "to_status": ""}
]
}
// finding_p1_created
{
"type": "finding_p1_created",
"count": 2,
"findings": [
{"id": 1532, "asset_id": 41},
{"id": 1533, "asset_id": 57}
]
}Asset Subscriptions
Clients can send {"type": "subscribe_asset", "uid": "<asset uid>"} or {"type": "unsubscribe_asset", "uid": "<asset uid>"} to join or leave the group of one asset. A socket can follow at most 100 assets (beyond that it gets {"type": "error", "detail": "Subscription limit reached (100)."}) and an invalid uid answers {"type": "error", "detail": "Invalid asset uid."}.
Note: The backend currently publishes no event to the asset groups, so a subscription receives nothing beyond the platform-wide and personal events.
Reconnection Strategy
WebSocket connections can be interrupted by network issues, server deployments, or client-side navigation. Clients should implement an automatic reconnection strategy to maintain a persistent connection. A recommended approach is exponential backoff starting with a 3-second delay, capped at a reasonable maximum.
- Initial delay: 3 seconds (recommended)
- Backoff strategy: double the delay after each failed attempt
- Maximum delay: 30 seconds to avoid excessively long waits
- Reset delay: return to the initial 3-second delay after a successful connection
let reconnectDelay = 3000; // Start with 3 seconds
const maxDelay = 30000; // Cap at 30 seconds
function connect() {
const ws = new WebSocket("wss://idp.example.com/ws/notifications/?token=" + encodeURIComponent(accessToken));
ws.onopen = () => {
console.log("Connected");
reconnectDelay = 3000; // Reset delay on successful connect
};
ws.onclose = () => {
console.log(`Reconnecting in ${reconnectDelay / 1000}s...`);
setTimeout(() => {
reconnectDelay = Math.min(reconnectDelay * 2, maxDelay);
connect();
}, reconnectDelay);
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
handleEvent(data);
};
}
connect();Event Summary
The following table summarizes all event types, their source, and when they are emitted.
- discovery_started: emitted when a discovery job begins execution
- discovery_completed: emitted when a discovery job finishes successfully, with the number of assets discovered
- discovery_failed: emitted when a discovery job fails with an error message
- service_health_changed: emitted once per business service health computation when at least one service changed status
- finding_p1_created: emitted when security findings become P1
- notification_created (personal group only): emitted when an in-app notification is created for the user
- notifications_read (personal group only): emitted when the user marks notifications as read
- report_ready / report_failed (personal group only): emitted to the user who requested the report
Note: All events are delivered as JSON text frames. Clients should parse the type field first to determine which handler to invoke for the event payload.