Skip to content

WebSocket Consumers

Authenticated Channels consumers that fan real-time device updates out to browsers. See WebSocket API for the message contract.

micboard.websockets.consumers

Source

Django Channels WebSocket consumers for real-time micboard updates.

This module provides WebSocket consumers for broadcasting device updates to connected clients.

MicboardConsumer

Bases: AsyncWebsocketConsumer

Source

WebSocket consumer for real-time device updates.

async connect() -> None

Source

Handle WebSocket connection.

async disconnect(code: int) -> None

Source

Handle WebSocket disconnection.

async receive(text_data: str | None = None, bytes_data: bytes | None = None) -> None

Source

Handle incoming messages from client.

async device_update(event: dict[str, Any]) -> None

Source

Send device update to WebSocket client.

async api_health_update(event: dict[str, Any]) -> None

Source

Forward a manufacturer API health update.

async device_status_update(event: dict[str, Any]) -> None

Source

Forward a persisted hardware status update.

micboard.websockets.authorization

Source

How long one WebSocket connection may act on an authorization decision.

Micboard re-reads a connection’s authorized routes from the database before forwarding an event, so a connection fails closed the moment a membership, a permission, or the account itself is revoked. Doing that once per frame makes the cost of the guarantee unbounded: a busy chassis broadcasts many frames per second, and the one inbound command a client may send takes the same path, so a client sets the pace of the server’s database work.

This module puts a declared number on both halves. An authorization decision is reused for a bounded time to live instead of re-read per frame, which turns revocation latency into a value a deployment chooses rather than an implicit “every frame”. Inbound commands are metered separately, so no client can force re-reads faster than its own budget allows.

Setting the time to live to zero restores per-frame re-reading for deployments that want revocation to take effect on the very next frame and can afford the queries.

authorization_ttl_seconds() -> float

Source

Return how long one authorization decision may be reused.

Returns:

  • float — A time to live in seconds, clamped to the supported range. Zero means every
  • float — forwarded event re-reads authorization from the database.

commands_per_minute() -> int

Source

Return how many inbound commands one connection may spend per minute.

Returns:

  • int — A whole number of commands, clamped to the supported range.

AuthorizationCache

Source

Reuse one connection’s authorized routes for a bounded time to live.

async authorized_groups() -> tuple[str, ...]

Source

Return the connection’s authorized routes, re-reading them only when stale.

Returns:

  • tuple[str, ...] — Every route the connection is currently allowed to receive events on.

invalidate() -> None

Source

Discard the cached decision so the next read goes to the database.

CommandBudget

Source

Meter how many inbound commands one connection may spend per window.

consume() -> bool

Source

Spend one command from the current window.

Returns:

  • bool — True when the command is within budget, False when it is not.

command_budget() -> CommandBudget

Source

Build one connection’s command budget from host configuration.

Returns:

  • CommandBudget — A budget metering the configured number of commands per minute.

micboard.websockets.routing

Source

Django Channels routing configuration for WebSocket connections.

This module defines the WebSocket URL routing for real-time micboard updates.